我有一个表格如下:
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'); } }
我在控制器中调用AdminEmployerForm,我想从AdminEmployerForm中删除AdminUserForm的电子邮件字段:
$form = $this->createForm(new AdminEmployerForm,$employer); //i want to do something like $form->remove('email')
解决方法
您必须获取嵌入的表单类型才能从中删除字段.
$form = $this->createForm(new AdminEmployerForm,$employer); // Get the embedded form... $adminUserForm = $form->get('user'); // ... remove its email field. $adminUserForm->remove('email');
不确定您的确切用例,但您可以考虑leveraging form events,因为它可能比在控制器中处理它更理想.