我的表格如下:
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-> remove()删除嵌入式表单中的字段?是否可以从控制器中删除嵌入表单的字段?
答案 0 :(得分:12)
您必须获取嵌入的表单类型才能从中删除字段。
$form = $this->createForm(new AdminEmployerForm, $employer);
// Get the embedded form...
$adminUserForm = $form->get('user');
// ... remove its email field.
$adminUserForm->remove('email');
不确定您的确切用例,但您可以考虑leveraging form events,因为它可能比在控制器中处理它更理想。