我有一个表格如下:
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');
}
}
我正在 Controller 中调用 AdminEmployerForm,并且我想从 AdminEmployerForm 中删除 AdminUserForm 的电子邮件字段:
$form = $this->createForm(new AdminEmployerForm, $employer);
//i want to do something like $form->remove('email')
如何使用 $form->remove() 删除嵌入表单中的字段?是否可以从 Controller 中删除嵌入表单的字段?
最佳答案
您必须获取嵌入式表单类型才能从中删除字段。
$form = $this->createForm(new AdminEmployerForm, $employer);
// Get the embedded form...
$adminUserForm = $form->get('user');
// ... remove its email field.
$adminUserForm->remove('email');
不确定您的确切用例,但您可以考虑 leveraging form events因为它可能比在 Controller 中处理它更理想。
关于symfony - 如何从 symfony 2 Controller 中删除嵌入表单中的表单字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26329148/