Reputation: 428
I have problem with dynamic forms.
I have two selects. First (name: controller) has a static values. The second one (name: driver) has dynamically values based on value of first select. When I'm changing the first select, new values is generating in second. Everything will be okay but the form didn't want to save. I've got this error:
This value is not valid.
My form builder:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('controller', null, array(
'attr' => array(
'id' => 'controller',
'class' => 'form-control'
)
))
->add('driver', 'choice', array(
'required' => false,
'choices' => array(),
'attr' => array(
'id' => 'driver',
'class' => 'form-control'
)
))
->add('update', 'submit', array(
'label' => 'form.btn.edit',
'attr' => array(
'class' => 'btn btn-default'
)
))
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'My\ControllerBundle\Entity\Event'
));
}
Why can't I save it?
Upvotes: 1
Views: 6017
Reputation: 120
I guess the problem is that the driver field has an empty array in choices. To be dynamic depending on other field, you should use Form Events:
http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html
And add the driver field dynamically using events.
Here is a post that can help you:
http://showmethecode.es/php/symfony/symfony2-4-dependent-forms/
Upvotes: 2