The DON’Ts of Symfony 2 DI on Abstract Classes

This is in response to the use of DI on Abstract Classes. The discussion originated here but I think it is a matter of importance so i am turning this into a blog post.

hi Bernhard, I don’t think he has listened you yet, judging for his latest post on collection forms he still passes does it twice:

Here:

$form = $this->createForm(new \Khepin\GolfBundle\Form\ParSetType($course));
 
// should be
$form = $this->createForm(new \Khepin\GolfBundle\Form\ParSetType(), $course);

And here:

 $builder->get('holes')->add('hole_'.$i, new HoleType($hole));
 
// should be
 $builder->get('holes')->add('hole_'.$i, new HoleType(), $hole); // <--- Is this possible!!! I wonder about the add signature!!

So I found out there is a space for a data option here:
So the code should now look like:

$builder->get('holes')->add('hole_'.$i, new HoleType(), array( 'data' => $hole ) );

I have not tried if this works but at least it has some support.

@Bernhard can you please help me out with the last part? how it should be done?

thanks! I will update this blog post as this is resolved.

Symfony 2 How To Disable CSRF on a Per Form Basis

Here are two ways to disable the CSRF in Symfony 2 Forms:

public function getDefaultOptions(array $options)
   {
       return array(
           'data_class'      => 'Acme\TaskBundle\Entity\Task',
           'csrf_protection' => false,  // <---- set this to false on a per Form Type basis
           //'csrf_field_name' => '_token',
           // a unique key to help generate the secret token
           //'intention'       => 'task_item',
       );
   }

And while creating the form:

$form = $this->createFormBuilder($users, array(
    'csrf_protection' => false,  // <---- set this to false on a per Form Instance basis
))->add(...)
;

If this has been helpful please donate, thanks!