Symfony2: Passing options to buildForm Symfony2: Passing options to buildForm symfony symfony

Symfony2: Passing options to buildForm


There is no need to create an additional param, just use options to pass custom data. In the following example, I made age attribute mandatory, you could have it optional or specify default value. Read more about OptionsResolver here

class ExampleType extends AbstractType{    public function buildForm(FormBuilderInterface $builder, array $options)    {        var_dump($options['age']);    }    public function setDefaultOptions(OptionsResolverInterface $resolver)    {        $resolver->setRequired(array(            'age'        ));    }}

Create:

$form = $this->get('form.factory')->create(new ExampleType(), $entry, array(    'age' => 13));