Reputation: 668
I would like to add widgets (checkboxes) in an already defined form (with configure method).
I can't add them in the definition of the form because the number of widgets varies (according to the object).
I see two ways of doing it :
Either pass a variable into the configure method of the form or maybe use embedded forms.
But which one is the right way ? Is there another solution ?
Thank you
Upvotes: 0
Views: 733
Reputation: 3588
I Agree with Don Pinkster on passing option and use it to configure form in configure() method.
But if need it or can't get the value when instanciating the class, you can use from anywhere :
$form->getWidgetSchema()->offsetSet($name, $widget);
$form->getValidatorSchema()->offsetSet($name, $validator)
The fact you use embedded forms or widget will not change that much, as you can do this after the form is initially configured :
$form->embedForm($name, $form2);
For just one checkbox I don't see advantages in using embedded form. In both cases, I suggest you do this in a public method from your form's class, to avoid exploding the form configuration in the action class or elsewhere.
Regards,
Upvotes: 1
Reputation: 366
The right way is to pass the object right into the options. In the form you can use the $this->getOption
method to retrieve the passed options.
Upvotes: 1