Customizing Validation

The default validation group for all resources is sylius, but you can configure your own validation.

How to customize validation?


You can browse the full implementation of these examples on this GitHub Pull Request.

Let’s take the example of changing the length of name for the Product entity - watch out the field name is hold on the ProductTranslation model.

In the sylius validation group the minimum length is equal to 2. What if you’d want to have at least 10 characters?

1. Create the config/validator/validation.yaml.

In this file you need to overwrite the whole validation of your field that you are willing to modify. Take this configuration from the src/Sylius/Bundle/ProductBundle/Resources/config/validation/ProductTranslation.xml - you can choose format xml or yaml.

Give it a new, custom validation group - [app_product].

            - NotBlank:
                groups: [app_product]
            - Length:
                min: 10
                max: 255
                groups: [app_product]


When using custom validation messages see here how to add them.

2. Configure the new validation group in the config/services.yaml.

# config/services.yaml
    sylius.form.type.product_translation.validation_groups: [app_product]
    sylius.form.type.product.validation_groups: [app_product] # the product class also needs to be aware of the translation's validation

Done. Now in all forms where the Product name is being used, your new validation group will be applied, not letting users add products with name shorter than 10 characters.


When you would like to use group sequence validation, like so. Be sure to use [Default] as validation group. Otherwise your getGroupSequence() method will not be called.

Good to know

See also

All the customizations can be done either in your application directly or in Plugins!