4

I am using Symfony, version 2.5.3, and I am trying to submit a form with Ajax. So, in my controller I have

  $form = $this->createForm(
      new MyFormType(), 
      $formvariable
  );

  $form->handleRequest( $request );

  if ($form->isValid()) {
      // do stuff
  } else {
      // other stuff
  } 

and my jQuery script could be similar to this:

    formdata = $('form[name=MyFormTypeForm]').serialize();

    $.ajax({
        type: "POST",
        url: // my url,
        data: formdata ,
        async: true,
        dataType: "json",
        success: function(response) 
        {
            // stuff here
        },
        error: function(XMLHttpRequest, textStatus, errorThrown)
        {
            // stuff there
        },
    });

This would work fine, but unfortunately I have to send other variables together with the fields of the form.
What I want to do in my jQuery is something like:

    formdata = $('form[name=MyFormTypeForm]').serialize();

    value      = { 
                  myvar1   : data1    ,
                  myvar2   : data2    ,
                  formdata : formdata ,
                };

    $.ajax({
        type: "POST",
        url: // my url,
        data: value ,
        async: true,
        dataType: "json",
        success: function(response) 
        {
            // stuff here
        },
        error: function(XMLHttpRequest, textStatus, errorThrown)
        {
            // stuff there
        },
    });

However, in this way $form->isValid() inside the controller always returns false.

Is there a way to fix this?
Thank you in advance

Alberto

Edit: Adding some additional details, in response to comments:

I cannot add some extra fields inside my form because the additional variables are used to generate the form itself.

More details of the controller:

    $em = $this->getDoctrine()->getManager();

      $action = $request->request->get('action',   null);
      $myentid = $request->request->get('myentid',   null);

      // Here I determine if I have to create a form for a new entity or an 
      // entity already present in the database
      if ( empty($myentid) )
      {
          // New entity
          $formvariable = new MyEntity();
      } else {
          // Retrieve entity from database
          $formvariable = $this->getDoctrine()->getManager()
              ->getRepository('MyBundle:MyEntity')
              ->find($myentid);
      }

      $form = $this->createForm(
          new MyFormType(), 
          $formvariable
      );

      // I am requesting a new form: render the form and send to page
      if ($action == "newform")
      {
          $response["code"] = $this->render('MyBundle:Controller:mytwigview.html.twig', array( 
              'form' => $form->createView(), 
              ))->getContent();
          return new Response(json_encode($response));

      }

      // The form has already been filled: save
      if ($action == "save")
      {
          $form->handleRequest( $request);

          if ($form->isValid()) {
              $em->flush();
          } else {
              echo "Errors: ".$form->getErrors();
          }
      }

// And here I return the save confirmation
7
  • What about adding the extra data as hidden inputs to the form? Commented Nov 10, 2014 at 11:48
  • Unfortunately this is not possible. I need this extra data to effectively generate the form, to which associate the formdata. Commented Nov 10, 2014 at 11:52
  • Wenn the extra data is used to generate the form... than it should be possible to inject it into the form itself... how are you generating it? Could you add this to your question? Commented Nov 10, 2014 at 11:54
  • Edited my original question to satisfy your request. Commented Nov 10, 2014 at 12:12
  • So it seems you are trying to merge the new and edit actions, right? Commented Nov 10, 2014 at 12:20

2 Answers 2

3

The controller should look something like this I would suggest:

/**
 * @Route("/myentity/{myentid}")
 */
public function newAndEditAction(Request $request, $myentid = null)
{
    $em = $this->getDoctrine()->getManager();

    // Here I determine if I have to create a form for a new entity or an 
    // entity already present in the database
    if ($myentid)
    {
        // Retrieve entity from database
        $formvariable = $this->getDoctrine()->getManager()
            ->getRepository('MyBundle:MyEntity')
            ->find($myentid);
    } else {
        // New entity
        $formvariable = new MyEntity();
    }

    $form = $this->createForm(
        new MyFormType(),
        $formvariable
    );

    if ($request->getMethod() == 'POST') { // save
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em->flush();
            // return some response
            return Response(...);
        }
    } else { // edit and new
        return new Response($this->render('MyBundle:Controller:mytwigview.html.twig', array(
            'form' => $form->createView(),
        ))->getContent());
    }
}

I made the id parameter optional for this action and decide whether to save/update and show new or edit form based on the id parameter and request method.

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

1 Comment

This seems not to answer how to process the form data submitted within the 'values' array?
0

I use the following to submit two forms at once via Ajax. It is also the answer to your question:

$.ajax({
    type: "POST",
    url: Routing.generate('ajax_report_processing_create_pdf'),
    data: $reportMainWrapper.find('form[name="report"]').serialize() + '&' +
    $reportProcessingForm.find('form').serialize(),
    dataType: "json"
})
    .done(function (data) {
        console.log('done');
        console.log(data);
    })
    .fail(function (jqXHR, textStatus, errorThrown) {
        console.log('fail');
        console.log(jqXHR);
    })
    .always(function () {
        console.log('always');
    });

As you can see, you can just use:

data: 'myvar1=' + data1 + '&' + 'myvar2=' + data2 + '&' + formdata,

As you see I did not 'formdata=' + formdata,. That is because your variable formdata already contains key-value data as such:

form[yourinput1]  = 'string1'
form[yourinput2]  = 'string2'

So in your controller you can now simply do a $form->handleRequest($request); and it will work instantly.

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.