symfonysymfony-validator

Generic Entity validation for all properties with type string in Symfony


I was wondering if there is some solution to the problem I will now say.

I have plenty of entities in the project (Symfony 4.4) and I need some generic solution to validate max length of all properties that have type="string" instead of going individually to each property and define Length validator (it would take a lot of time for that). So the whole process would look like this: Generic entity validation that goes through all the related entity properties -> picks up the properties that are text type with length definition -> validates the actual property value against the length definition.


Solution

  • You can acheive that directly via your validation.yml, this feature is introduced in SF 4.3

    # Enables validator auto-mapping support.
    # For instance, basic validation constraints will be inferred from Doctrine's metadata.
    #auto_mapping:
    #    App\Entity\: []