问题
Possible Duplicate:
Symfony2 validation doesn’t work when Entity Relationships/Associations
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 ));
}
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?
回答1:
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
));
}
来源:https://stackoverflow.com/questions/13037203/symfony2-validation-of-form-collection-does-not-work