场景:我有一个有2个选择的表单.当用户从第一个选择中选择某个内容时,第二个选择将填充新值.这部分工作正常.
但是表单没有得到验证,因为它包含初始表单中不允许的一些选项.
形成:
<?php
class MyType extends AbstractType
{
private $category;
public function __construct($category = null)
{
$this->category = $category;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('category', 'choice', array(
'choices' => array(
'foo' => 'foo',
'bar' => 'bar'
)
);
$builder->add('template', 'choice', array(
'choices' => $this->loadChoices()
);
}
private function loadChoices()
{
// load them from DB depending on the $this->category
}
}
Run Code Online (Sandbox Code Playgroud)
最初的类别是foo.因此foo的模板被加载并设置为选项.但是如果用户选择bar,则会加载条形模板.但表单仍然有foo选择,不会验证.
解决这个问题的最佳方法是什么?
我找到的一种方法是在控制器中重新启动表单:
<?php
$form = $this->createForm(new MyType());
if ($request->getMethod() === …Run Code Online (Sandbox Code Playgroud)