Class constraint validator in form type class?
Asked Answered
D

2

9

In the documentation it lists examples for applying a class validation constraint to Annotations, XML and YAML but not for PHP.

Since the validation is not applied to a property, where do I add this class validation constraint to the whole class inside a Symfony form type?

Doggery answered 23/3, 2017 at 16:19 Comment(1)
I'm looking exactly for the same, good questionParsec
P
5

I think I've found a solution here: https://knpuniversity.com/screencast/question-answer-day/custom-validation-property-path There is a tip what says:

If your form lives in a form type class, simply add the constraints key to the setDefaulOptions method.

Parsec answered 25/11, 2017 at 22:58 Comment(0)
F
0

I couldn't find the setDefaultOptions mentioned on the other answer, but managed to get it to work with OptionsResolver::setDefaults. It must have something to do with Symfony's version.

Something like this should do the trick:

$resolver->setDefaults([
        'constraints' => [
            // list of constraints,
        ],
    ]);
Feldspar answered 30/12, 2022 at 15:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.