Symfony2: Validation of form collection does not work [duplicate]

折月煮酒 提交于 2020-01-02 21:00:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!