Question

I call a form with ajax, instead of display it in page.

Everything is working, except 1 strange thing.

When i call the form via ajax and add it to the page, defaults values are not in fields.

But if i call the form via normal url, default value are set...

$builder
        ->add('email', null,array('data' => 'default value')
        ->add('type_form', null, array('data' => 'default value', 'property_path'=> false))
        ->add('list_choice', 'choice', array(
            'choices' => $options['list'],
            'expanded'=>true,
            'multiple'=>true,
        ));

The field email and type_form do not display the 'default value' as value except if i call the form via his url, and not via ajax.

Is there a specific way to call form via ajax ? Thanks for your answers.

Here the way i create the form in my controller

$this->container->get('templating')->render('MyappSiteBundle:Contributions:filter_themes_form.html.twig', array(
'form'=> $form->createView(),
'type'=> $type ));

$response = new Response(json_encode(array('form'=> $response_form)));


$response->headers->set('Content-Type', 'application/json');
return $response;

Then i was calling the form...

Was it helpful?

Solution

I found the solution. I was clearing form data before to send it:

if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);
        if ($form->isValid()) {
            $data = $form->getData();
            return $data;
        }
    }

Then I was calling the form.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top