μλ νμΈμ μ¬λ¬λΆ,
컨νΈλ‘€λ¬μ μμμ μ μΆνλ €κ³ νλλ° μμμμ ν΄λΉ μ€λ₯ λ©μμ§λ₯Ό λ°μ μ μμ΅λλ€.
λ¬Έμ(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;
}
}
λ΄κ° λ μλͺ»νκ³ μλ???
미리 κ°μ¬λ립λλ€
μ λ°μ΄νΈ: λλ μ¬κΈ°(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
κ°μ₯ μ μ©ν λκΈ
λλ κ°μ λ¬Έμ κ° μμλ€. κ·Έλ¬λ 곡μ symfony λ¬Έμμμλ submit()μ΄ λ μ΄μ μ¬μ©λμ§ μκΈ° λλ¬Έμ handleRequest() λ©μλλ§ μ¬μ©νλλ‘ μ μνλ κ²μ΄ μ΄μν©λλ€. http://symfony.com/doc/current/cookbook/form/direct_submit.html#cookbook -form-call-submit-directly