1

可能重复:
当实体关系/关联时 Symfony2 验证不起作用

我有一个表单 PageFormType,它只有一个名为“条目”的字段,没有数据类。

public function buildForm(FormBuilderInterface $builder, array $options) {

  $builder->add('entries', 'collection', array('type' => new EntryFormType() );
}

public function setDefaultOptions(OptionsResolverInterface $resolver) {
    $resolver->setDefaults(array('data_class' => null  ));
}

表单的设置效果很好,我看到了呈现的 EntryFormTypes 的每个条目和每个字段,但是当验证我的 PageFormType 时,它​​总是有效的。验证单个 EntryFormType 可以,但我喜欢一次验证所有嵌入的表单。这有可能吗?

4

1 回答 1

3

您必须为您的表单分配验证(请参阅:http ://symfony.com/doc/current/book/forms.html#adding-validation )。在您的情况下,您可能想要这样的东西,使用Valid-Constraint

use Symfony\Component\Validator\Constraints\Valid;

...

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $collectionConstraint = new Collection(array(
        'entities' => new Valid(),
    ));

    $resolver->setDefaults(array(
        'validation_constraint' => $collectionConstraint
    ));
}
于 2012-10-23T19:42:59.250 回答