How to remove form field in embedded forms from Symfony 2 controller

I have the following form:

class AdminEmployerForm extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('firstName', 'text') ->add('user', new AdminUserForm()); } } class AdminUserForm extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('username', 'text') ->add('email', 'text'); } } 

I call AdminEmployerForm in the controller and I want to remove the AdminUserForm email field from AdminEmployerForm:

 $form = $this->createForm(new AdminEmployerForm, $employer); //i want to do something like $form->remove('email') 

how can i use $ form-> remove () to remove a field in an inline form? Is it possible to remove the embedded form field from the controller?

+5
source share
1 answer

You will need to get the built-in type of the form in order to remove the field from it.

 $form = $this->createForm(new AdminEmployerForm, $employer); // Get the embedded form... $adminUserForm = $form->get('user'); // ... remove its email field. $adminUserForm->remove('email'); 

Not sure about your specific use case, but you might consider using form events , as it might be more ideal than handling this in a controller.

+10
source

Source: https://habr.com/ru/post/1204521/


All Articles