我在表单中有一个名为 *sub_choice* 的选择字段类型,它的选项将根据父级选择字段 *parent_choice* 中所选值的不同通过 AJAX 来动态加载。加载选项运行良好,但是当提交时验证 sub_choice 值时,出现了问题。由于提交的值不在构建 sub_choice 字段时的选项中,因此会出现“该值无效”的验证错误。那么我是否有一种方法可以正确验证 sub_choice 字段的提交值呢?下面是构建我的表单的代码。我正在使用 Symfony 2.1。
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('parent_choice', 'entity', array(
'label' => 'Parent Choice',
'class' => 'Acme\TestBundle\Entity\ParentChoice'
));
$builder->add('sub_choice', 'choice', array(
'label' => 'Sub Choice',
'choices' => array(),
'virtual' => true
));
}