Option does not exist error when passing through option from controller to embedded Symfony form Option does not exist error when passing through option from controller to embedded Symfony form symfony symfony

Option does not exist error when passing through option from controller to embedded Symfony form


As you have discovered, each form type has a predefined list of options. Adding a new option requires a slight adjustment. The actual method has changed over the course of Symfony development so you may come across some older depreciated solutions.

The most up to date solution is discussed here: http://symfony.com/blog/new-in-symfony-2-7-form-and-validator-updates#deprecated-setdefaultoptions-in-favor-of-configureoptions

So basically add

public function configureOptions(OptionsResolver $resolver){    $resolver->setDefaults(array(        'data_class' => 'AppBundle\Entity\Whatever',        'numOfHoles' => 0,    ));

To your form types and you should be good to go.


I saw in the video that you need to create a form type extension to "invent" options:

https://symfonycasts.com/screencast/symfony-forms/form-type-extension#play

"It turns out that you can't just "invent" new options and pass them: each field has a concrete set of valid options. But, in TextareaSizeExtension, we can invent new options. "

For example when needed to add rows atribute, in extension done this:

   public function buildView(FormView $view, FormInterface $form, array $options)    {        $view->vars['attr']['rows'] = $options['rows'];    }public function configureOptions(OptionsResolver $resolver)    {        $resolver->setDefaults([            'rows' => 10        ]);    }

And now in buildForm method when you add a field you can pass option this way:

->add('content', null, ['rows' => 15])

This should at least work with symfony 4 as in the video was talked, probably 3.4 also.

Just watched another video https://symfonycasts.com/screencast/symfony-forms/form-options-data and saw doing same as Cerad answered. So now as I understand we need formExtention only if we want to extend third party form type.


Try doing this:

$numOfHoles = array('numOfHoles' => $options['numOfHoles']));

then:

$oForm = $this->createForm(new CompanyForm($numOfHoles));

Then you need to call it before your buildForm:

public function __construct($contacts)){$this->vnumOfHoles = $numOfHoles;}