Fosrestbundle: 양식 μœ νš¨μ„± 검사에 였λ₯˜ λ©”μ‹œμ§€κ°€ ν‘œμ‹œλ˜μ§€ μ•ŠμŒ

에 λ§Œλ“  2015λ…„ 04μ›” 15일  Β·  7μ½”λ©˜νŠΈ  Β·  좜처: FriendsOfSymfony/FOSRestBundle

μ•ˆλ…•ν•˜μ„Έμš” μ—¬λŸ¬λΆ„,

μ»¨νŠΈλ‘€λŸ¬μ— 양식을 μ œμΆœν•˜λ €κ³  ν•˜λŠ”λ° μ–‘μ‹μ—μ„œ ν•΄λ‹Ή 였λ₯˜ λ©”μ‹œμ§€λ₯Ό 받을 수 μ—†μŠ΅λ‹ˆλ‹€.

λ¬Έμ„œ(http://symfony.com/doc/current/bundles/FOSRestBundle/2-the-view-layer.html#forms-and-views)에 λ”°λ₯΄λ©΄

{
  "code": 400,
  "message": "Validation Failed";
  "errors": {
    "children": {
      "username": {
        "errors": [
          "This value should not be blank."
        ]
      }
    }
  }

κ·ΈλŸ¬λ‚˜ λ‚΄ λŒ€λ‹΅μ€ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

{
  "children": {
    "username": [],
  }
}

λ‚΄ config.yml

fos_rest:
    disable_csrf_role: IS_AUTHENTICATED_ANONYMOUSLY
    param_fetcher_listener: true
    # formato default caso nΓ£o seja informado
    routing_loader:
        default_format: json
    view:
        # registra os mimi types permitidos no header da request
        mime_types: 
            json: ['application/json', 'application/json;version=1.0', 'application/json;version=1.1']
        view_response_listener: 'force'
        formats:
            xml:  true
            json: true
        templating_formats:
            html: true
    format_listener: true
    exception:
        codes:
            'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
            'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT
        messages:
            'Symfony\Component\Routing\Exception\ResourceNotFoundException': true
    allowed_methods_listener: true
    access_denied_listener:
        json: true
    body_listener: true

λ‚΄ 컨트둀러:

<?php

namespace AppBundle\Controller;

use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Controller\Annotations as Rest;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\Constraints as Assert;

class SecurityController extends FOSRestController
{

    /**
     * Auth
     *
     * @ApiDoc(
     *  resource=true,
     *  description="Do the authentication",
     *  parameters={
     *      {"name"="username", "dataType"="string", "required"=true, "description"="username from user"}
     *  },
     *  statusCodes={
     *      200="Returned when successful"
     *  }
     * )
     *
     * @Rest\Post()
     * @Rest\View()
     */
    public function authAction(Request $request)
    {
        $form = $this->createFormBuilder()
                ->add('username', 'text', array('constraints' => new Assert\NotBlank()))
                ->getForm();

        $form->handleRequest($request);
        if ($form->isValid()) {
            // do something
        }

        return $form;

    }

}

λ‚΄κ°€ 뭘 잘λͺ»ν•˜κ³  μžˆλ‹ˆ???

미리 κ°μ‚¬λ“œλ¦½λ‹ˆλ‹€

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

λ‚˜λŠ” 같은 λ¬Έμ œκ°€ μžˆμ—ˆλ‹€. κ·ΈλŸ¬λ‚˜ 곡식 symfony λ¬Έμ„œμ—μ„œλŠ” submit()이 더 이상 μ‚¬μš©λ˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— handleRequest() λ©”μ„œλ“œλ§Œ μ‚¬μš©ν•˜λ„λ‘ μ œμ•ˆν•˜λŠ” 것이 μ΄μƒν•©λ‹ˆλ‹€. http://symfony.com/doc/current/cookbook/form/direct_submit.html#cookbook -form-call-submit-directly

λͺ¨λ“  7 λŒ“κΈ€

μ—…λ°μ΄νŠΈ: λ‚˜λŠ” μ—¬κΈ°(https://github.com/FriendsOfSymfony/FOSRestBundle/issues/738#issuecomment-45312857)μ—μ„œ μ†”λ£¨μ…˜μ„ μ‹œλ„ν–ˆκ³  μž‘λ™ν•©λ‹ˆλ‹€.

λ‚˜λ„ μ‹œλ„ν–ˆλ‹€

$form->submit(array(
            'username' => $request->get('username')
        )); 

μ–΄μ¨Œλ“  μž…λ ₯ λ°μ΄ν„°μ˜ μœ νš¨μ„±μ„ κ²€μ‚¬ν•˜λŠ” 더 쒋은 λ°©λ²•μ΄μ–΄μ•Όν•œλ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€ ...

감사 ν•΄μš”

λ‚˜λŠ” 같은 λ¬Έμ œκ°€ μžˆμ—ˆλ‹€. κ·ΈλŸ¬λ‚˜ 곡식 symfony λ¬Έμ„œμ—μ„œλŠ” submit()이 더 이상 μ‚¬μš©λ˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— handleRequest() λ©”μ„œλ“œλ§Œ μ‚¬μš©ν•˜λ„λ‘ μ œμ•ˆν•˜λŠ” 것이 μ΄μƒν•©λ‹ˆλ‹€. http://symfony.com/doc/current/cookbook/form/direct_submit.html#cookbook -form-call-submit-directly

@sepikas-antanas : 제좜 κΈ°λŠ₯에 λŒ€ν•œ μž…λ ₯ λ§€κ°œλ³€μˆ˜λ‘œ μš”μ²­ 객체λ₯Ό μ‚¬μš©ν•˜λŠ” 것은 κΈ°λŠ₯ μžμ²΄κ°€ μ•„λ‹ˆλΌ 더 이상 μ‚¬μš©λ˜μ§€ μ•ŠλŠ”λ‹€κ³  μƒκ°ν•©λ‹ˆκΉŒ?

jmsserializer λ˜λŠ” symfony 직렬 λ³€ν™˜κΈ°λ₯Ό μ‚¬μš©ν•©λ‹ˆκΉŒ?

그리고 @sepikas-antanasκ°€ Requestλ₯Ό μ†μ„±μœΌλ‘œ μ „λ‹¬ν•˜κΈ°λ§Œ ν•˜λ©΄ submit은 더 이상 μ‚¬μš©λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

λ‚˜λŠ” μ •ν™•νžˆ 같은 λ¬Έμ œκ°€ 있고 jmsserializerλ₯Ό μ‚¬μš©ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€. μ–΄λ””μ—μ„œ μ™”λŠ”μ§€ μ•Œ 수 μžˆμŠ΅λ‹ˆκΉŒ?

당신은 λ°˜ν™˜ν•΄μ•Όν•©λ‹ˆλ‹€

$view = $this->view($form->getErrors(true));
return $this->handleView($view);

λ‚˜λŠ” 이것을 λ‹€μŒκ³Ό 같이 ν•΄κ²°ν–ˆλ‹€.

    $user = new User;
    $form = $this->createForm(UserType::class, $user);
    $view = View::create();

    $form->submit($request->request->all());
    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();

        $em->persist($user);
        $em->flush();

        $view->setData($form->getData());
    } else {
        $view->setData($form);
    }

    return $this->handleView($view);

CSRF μœ νš¨μ„± 검사λ₯Ό λΉ„ν™œμ„±ν™”ν•˜λŠ” 것을 μžŠμ§€ λ§ˆμ‹­μ‹œμ˜€(https://symfony.com/doc/master/bundles/FOSRestBundle/2-the-view-layer.html#csrf-validation).

fos_rest:
    disable_csrf_role: ROLE_API
    #disable_csrf_role: IS_AUTHENTICATED_ANONYMOUSLY #just for testing
이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰