0

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)

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the feedback. I am looking to using custom validators. Any idea how to use Propel to know which field or element of a collection was updated?
I don't use Propel, but in Doctrine it's call unit of work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.