简体   繁体   中英

Symfony2: validation of form collection

I have a collection of element within a form. I want to apply validation rules on these elements, but the rules have to be different for the elements updated and the newly created element.

Ex: a collection of 4 elements. Adding the 5th element, a check has to be done on this element only. Updating the 3th element, another check has to be done, on this element only. Deleting the 1st element, a 3th check shall be done.

How is it possible ? (note: I use Propel)

You can use different validation groups in order to do that. In your form type you can determine which validation group to use with a Closure.

'validation_groups' => function(FormInterface $form) {
        $data = $form->getData();
        if (Client::TYPE_PERSON == $data->getType()) {
            return array('person');
        }

        return array('company');
    },

see http://symfony.com/doc/current/book/forms.html#groups-based-on-the-submitted-data and http://symfony.com/doc/current/book/forms.html#validation-groups

If it's not enough you can create your own validator on class constraint : http://symfony.com/doc/current/cookbook/validation/custom_constraint.html#class-constraint-validator and use Propel to know which field was updated.

Hope it's helpful.

Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM