Symfony2:从Ajax表单提交保存记录

时间:2015-11-29 08:00:30

标签: ajax rest symfony fosrestbundle nelmiocorsbundle

我完全迷失了。在它开始毫无意义之前,人们可以阅读的文档非常多。

我希望能够保存从Symfony应用程序外部传递的表单数据。我已经安装了FOSRestBundle,JMSSerializerBundle,NelmioCorsBundle等。

首先,我有一个如下所示的FormType:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('title')
        ->add('requestDate')
        ->add('deliverDate')
        ->add('returnDate')
        ->add('created')
        ->add('updated')
        ->add('contentChangedBy')
    ;
}

然后我有一个包含POST方法的REST控制器,它应该存储新记录:

class AvRequestController extends Controller
{
    ...
    public function postAvrequestAction(Request $request){
        $entity = new AvRequest();


        $form = $this->createForm(new AvRequestType(), $entity);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();

            return new \Symfony\Component\HttpFoundation\JsonResponse($entity, Codes::HTTP_CREATED);
        }

        return new \Symfony\Component\HttpFoundation\JsonResponse($request, 400); 
    }
}

以下是使用模拟ajax表单数据的测试:

$('#postform').submit(function(event){
    event.preventDefault();
    console.log("submitted");

    ajaxObject = {
        url: $("#postform").attr("action"),
        type: 'POST', // Can be GET, PUT, POST or DELETE only
        dataType: 'json',
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        contentType: "application/json; charset=UTF-8",
        data: JSON.stringify({"id":2, "title":"billabong", "requestDate":"2000-01-01 11:11:11", "deliverDate": "2000-01-01 11:11:11", "returnDate": "2000-01-01 11:11:11", "created": "2000-01-01 11:11:11", "updated": "2000-01-01 11:11:11", "content_changed_by":"cpuzzuol"})
    };

    // ... Add callbacks depending on requests

    $.ajax(ajaxObject)
        .done(function(data,status,xhr) {
            console.log( two );
        })
        .fail(function(data,status,xhr) {
            console.log( status );
        })
        .always(function(data,status,xhr) {
            console.log( data );
        });


    console.log("END");
});

当我提交表单时,我的POST方法会触发400 Bad Request。更糟糕的是,我的$ request包一直是空的:

{"attributes":{},"request":{},"query":{},"server":{},"files":{},"cookies":{},"headers":{}}

如果我这样做

$request->getContent()

我得到了我的字符串化数据:

"{\u0022id\u0022:2,\u0022title\u0022:\u0022billabong\u0022,\u0022requestDate\u0022:\u00222000-01-01 11:11:11\u0022,\u0022deliverDate\u0022:\u00222000-01-01 11:11:11\u0022,\u0022returnDate\u0022:\u00222000-01-01 11:11:11\u0022,\u0022created\u0022:\u00222000-01-01 11:11:11\u0022,\u0022updated\u0022:\u00222000-01-01 11:11:11\u0022,\u0022content_changed_by\u0022:\u0022cpuzzuol\u0022}"

我读过这可能与FOSRestBundle的“身体监听器”有关,但我已经启用了:

body_listener: true 

更新

body_listener似乎根本没有发挥作用。正如下面的答案所述,您必须创建一个空白名称的表单,因为您从系统外部提交的表单不具有通常在Symfony内部生成的名称。另外,如果您最初没有设置,请务必关闭CSRF。

1 个答案:

答案 0 :(得分:1)

表单isValid也会检查CSRF token validation。您可以在csrf token validation中关闭AvRequestType

//...
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'AppBundle\Entity\AvRequest',
        'csrf_protection' => false
    ));
}
//...

另外,我建议您的表单为name。 isValid还会检查您的form name

// form without name
public function getName()
{
    return '';
}

$form = $this->get('form.factory')->createNamed('', new AvRequestType(), $avRequest);

如果你想创建实体,你应该发送data没有id(来自JS)。

我使用过" JMS序列化器"将我的实体序列化为json。     //控制器

public function postAvRequestAction(Request $request)
{
    $avRequest = new AvRequest();

    $form = $this->createForm(new AvRequestType(), $avRequest);
    $form->handleRequest($request);

    $form = $this->get('form.factory')->createNamed('', new AvRequestType(), $avRequest);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($avRequest);
        $em->flush();

        $serializer = $this->get('serializer');
        $serialized = $serializer->serialize($avRequest, 'json');
        return new Response($serialized);
    }

    return new JsonResponse(array(
        'errors' => $this->getFormErrors($form)
    ));
}

protected function getFormErrors(Form $form)
{
    $errors = array();

    foreach ($form->getErrors() as $error) {
        $errors['global'][] = $error->getMessage();
    }

    foreach ($form as $field) {
        if (!$field->isValid()) {
            foreach ($field->getErrors() as $error) {
                $errors['fields'][$field->getName()] = $error->getMessage();
            }
        }
    }

    return $errors;
}