我有一个 Symfony 表单,它是从控制器中的 createFormBuilder() 创建的。但是在两个 IF 语句下,表单应该带有两个不同的字段集。我的 Symfony 版本是 3.4。/src/AppBundle/Controller/DefaultController.php例如:这是基本形式。$form = $this->createFormBuilder() ->add('name', TextType::class, ['required' => true]]) ->add('email', EmailType::class, ['required' => true]]) ->getForm();if ($form->isSubmitted() && $form->isValid()) { ... ... ...}现在,当条件变为真时,我需要向这个基本表单添加更多字段。下面显示的是我所做的,它给了我一个错误。$form = $this->createFormBuilder() ->add('name', TextType::class, ['required' => true]]) ->add('email', EmailType::class, ['equired' => true]]);if($a==true){ $form->add('username', TextType::class, ['required' => true]]) ->add('password', PasswordType::class, ['required' => true]]);}$form->getForm();if ($form->isSubmitted() && $form->isValid()) { ... ... ...}错误是:试图调用类“Symfony\Component\Form\FormBuilder”的名为“isSubmitted”的未定义方法。我怎样才能做到这一点?在不同条件下在多个场合追加字段?
1 回答
冉冉说
TA贡献1877条经验 获得超1个赞
尝试:
$formbuilder = $this->createFormBuilder()
->add('name', TextType::class, ['required' => true]])
->add('email', EmailType::class, ['equired' => true]]);
if($a==true){
$formbuilder->add('username', TextType::class, ['required' => true]])
->add('password', PasswordType::class, ['required' => true]]);
}
$form = $formbuilder->getForm();
if ($form->isSubmitted() && $form->isValid()) {
...
...
...
}
- 1 回答
- 0 关注
- 168 浏览
添加回答
举报
0/150
提交
取消