2

I'm trying to send other data along with a submitted form to a controller inside Symfony2.

When I try this like:

$("#submit_btn").on("click", function(e){
        e.preventDefault();
        $.ajax({
            type: "POST",
            url: post_url,
            data: form.serialize()
        });
    });

I see i got a successful POST request followed by a redirect as intended inside the controller action, If IsValid() returned true.

But when I try to send other data with the form like:

$("#submit_btn").on("click", function(e){
        e.preventDefault();
        $.ajax({
            type: "POST",
            url: post_url,
            data: { form: form.serialize(), otherdata: "test" }
        });
    });

I do not get the redirection 302 response. instead I get only one 200 response when means IsValid() method returned false. My question here how to not only send form, but also other data with it ?

Here's my controller action:

public function postOverviewAction(Request $request, $id)
    {
        $overview = $this->get("doctrine_mongodb")->getRepository("GbrBEBundle:Overview")->findOneById($id);
        $overview_photos = $this->get("doctrine_mongodb")->getRepository("GbrBEBundle:OverviewPhoto")->findAll();
        $form = $this->createForm(new OverviewType(), $overview);
        $form->handleRequest($request);

        $height = $form->get("coordinate_height")->getData();
        $width = $form->get("coordinate_width")->getData();
        $x = $form->get("coordinate_x")->getData();
        $y = $form->get("coordinate_y")->getData();

        if($form->isValid())
        {
            $overview->setCropCoordinates(array('height' => $height, 'width' => $width, 'x' => $x, 'y' => $y));
            $dm = $this->get("doctrine_mongodb")->getManager();
            $dm->persist($overview);
            $dm->flush();
            return $this->redirect($this->generateUrl("gbr_be_get_overview"));
        }
        return $this->render("GbrBEBundle:Default:overview.html.twig", array(
            "form" => $form->createView(),
            "overview" => $overview,
            "overview_photos" => $overview_photos,
        ));
    }
Rafael Adel
  • 7,673
  • 25
  • 77
  • 118

2 Answers2

1

You can add an unmapped field to the form:

How do I add an unbound field to a form in Symfony which is otherwise bound to an entity?

Setting to type text should prove the most versatile.

You can also create a collection field as the unmapped field, and assign the text type to it. This will allow you to have multiple additional text data on the receiving end.

http://symfony.com/doc/current/reference/forms/types/collection.html

Community
  • 1
  • 1
Flosculus
  • 6,880
  • 3
  • 18
  • 42
  • First, Thanks for your answer. My problem is not with the unmapped fields. It's with sending a request that not only include the form data but also other variables set inside jquery. Symfony have some mechanism on calling `IsValid()` method. So apparently it thinks that the form is not posted at all, While in fact it's posted with other variables inside a collection created in jQuery. – Rafael Adel Dec 11 '13 at 08:38
  • Are you also sending the token? – Flosculus Dec 11 '13 at 08:43
0

may be try to change:

 data: { form: form.serialize(), otherdata: "test" }

by data: form.serialize()+'&otherdata=test',

i think its because .serialize() return a string.

D'Ums
  • 13
  • 4