1

I need to send a data from a Symfony form with an Ajax call with an extra Data containing options for the controller.

I'm working on a custom framework including the Symfony form component.

So I try in my JS to send values like this :

$.ajax
(
    {
        type: "POST",
        url: '/myUrl',
        data: 
        {
            'formData' : $(this).serialize(),
            'extraData' : {'test1' : 'test1', 'test2' : 'test2'}
        },
        success: function(result)
        {
            // do something
        }
    }
);

How can I get my form data in my controller to let the component correctly understand it ? This code won't work :

$formData = $this->getRequest()->request('formData');
$form->bind($formData);

Thank you. I hope my question is clear enough

Edit: I finally integrated the data into hidden fields in my form. The form doesn't declare any data_class, as the ORM we use is a really custom one. Thanks for the answers

2
  • Why not $form->bind($_POST['formData']); ? Commented May 19, 2014 at 11:13
  • I correctly get the data by using the request. The problem is that the form is not validated... It looks like it can't associate the fields of the form and the fields sent in the request. Commented May 19, 2014 at 12:06

2 Answers 2

1

try $form->submit($formData);

http://symfony.com/doc/current/cookbook/form/direct_submit.html

Sign up to request clarification or add additional context in comments.

Comments

1

If your form has a data_class option, you can't bind / handle the request directly with extra_values.

Your form expects the variables that you already defined on your formtype.

Use it like this:

if($request->isXmlHttpRequest()) {
    $form->handleRequest($request);

    if($form->isValid()) {
        // To get extra data;
        $extra_data = $request->request->get('extraData');
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.