Check the code sample below
Father form
class FatherType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add( 'children', 'collection', array( 'type' => new ChildFormType(), 'allow_add' => true, 'allow_delete' => true, 'by_reference' => false, 'required' => false, 'cascade_validation' => true //important to be added ) ); } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults( array( 'data_class' => 'Company/SomeBundle/Form/Entity/FatherFormEntity', 'cascade_validation' => true, //important to be added ) ); } public function getName() { return 'father'; } }Child form
class ChildType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add( 'grandchildren', 'collection', array( 'type' => new GrandchildFormType(), 'allow_add' => true, 'allow_delete' => true, 'by_reference' => false, 'required' => false, 'cascade_validation' => true //important to be added ) ); } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults( array( 'data_class' => 'Company/SomeBundle/Form/Entity/ChildFormEntity', 'cascade_validation' => true, //important to be added ) ); } public function getName() { return 'child'; } }Grandchild form
class GrandchildType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add( 'attribute', 'text' ); } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults( array( 'data_class' => 'Company/SomeBundle/Form/Entity/GrandchildFormEntity', 'cascade_validation' => true, ) ); } public function getName() { return 'grandchild'; } }
References: https://github.com/symfony/symfony/issues/5204
I hope it helped you!
Robert Rusu
No comments:
Post a Comment