Symfony2:表单收集的验证不起作用 [英] Symfony2: Validation of form collection does not work
问题描述
可能重复:
当实体关系/协会时,Symfony2验证无效
Possible Duplicate:
Symfony2 validation doesn’t work when Entity Relationships/Associations
我有一个Form PageFormType,只有一个名为entries且没有数据类的字段。
I have a Form PageFormType, that has just one field named "entries" and no data class.
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的每个字段都呈现,但是当validationg我的PageFormType时,它总是有效的。单个EntryFormType的验证将有效,但是我想立刻验证所有的嵌入表单。
The setup of the form works great, I see every entry and every field of the EntryFormTypes rendered, but when validationg my PageFormType, it is always valid. Validation of a single EntryFormType would work, but I like to validate all my embedded forms at once. Is this somehow possible?
推荐答案
您必须为表单指派验证(请参阅: http://symfony.com/doc/current/book/forms.html#adding-validation ) 。在你的情况下,你可能想要这样的东西,使用有效约束 :
You have to assign validation to your form (see: http://symfony.com/doc/current/book/forms.html#adding-validation). In your case you probably want something like this, using the 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
));
}
这篇关于Symfony2:表单收集的验证不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!