How to associate a Symfony2 form with an array (not a request)?

Say I have an HTML form with a bunch of fields. Some fields belong to Product, some to Order, some to Other. When the form is submitted, I want to accept this request, and then create forms in Symfony for the product, order, and more. Then I want to get partial form data and bind it to the corresponding forms. An example would be something like this:

$productArray = array('name'=>$request->get('name'));
$pf = $this->createForm(new \MyBundle\Form\ProductType(), $product);
$pf->bind($productArray);
if($pf->isValid()) {
  // submit product data
}

// Do same for Order (but use order data)

// Do same for Other (but use other data)

The fact is that when I try to do this, I can not get the $ form-> isValid () method. The bind () operation seems to fail. I have a suspicion that this may be related to the form token, but I'm not sure how to fix it. Again, I create my own HTML form in the view (I did not use form_widget (), causing all the difficulties it would take to combine a bunch of FormTypes into one of them). I just want a very simple way to use a basic HTML form with the Symfony form set of functions.

Can someone tell me that this is possible even with Symfony and how can I do this?

+5
source share
2 answers

You need to disable the CSRF token for manual data binding.

csrf_protection .

:

$pf = $this->createForm(new \MyBundle\Form\ProductType(), $product, array(
    'csrf_protection' => false
));
+2

, , :

// Main form
$builder
    ->add('product', new ProductType)
    ->add('order', new OrderType);

, , . , .

?

0

All Articles