Customizing Validation
Last updated
Was this helpful?
Last updated
Was this helpful?
The default validation group for all Sylius resources is sylius
, but you can define and configure your own validation logic.
Letβs say you want to change the minimum length of the name
field for a Product
.
Important: the name
field is located in the ProductTranslation
model.
In the default sylius
validation group, the minimum length is 2
.
Suppose you want to enforce at least 10 characters.
Create a file:
β‘οΈ config/validator/ProductTranslation.yaml
or config/validator/ProductTranslation.xml
In this file, youβll override the validation rules for your target field. You can base your structure on the original file located .
Here are both examples using the new group app_product
:
Add the following to your config/services.yaml
:
Now, the new validation group will be applied in all forms where the Product is used. This means products with names shorter than 10 characters will no longer be accepted.
Some parts of Sylius do not use the standard validation mechanism via sylius.form.type.*.validation_groups
. These include components like ShippingMethod rules, Promotion rules, and Promotion actions, where the configuration is a nested array structure instead of a simple form-data mapping.
This means you must apply validation directly to the configuration structure using Symfony constraints in a different way.
Here are the models that require this special handling:
Sylius\Component\Shipping\Model\ShippingMethodRule
Sylius\Component\Shipping\Model\ShippingMethod
Sylius\Component\Promotion\Model\CatalogPromotionAction
Sylius\Component\Promotion\Model\CatalogPromotionScope
Sylius\Component\Promotion\Model\PromotionRule
Sylius\Component\Promotion\Model\PromotionAction
Sylius\Component\Promotion\Model\PromotionCoupon
Sylius\Component\Addressing\Model\ZoneMember
Suppose you want to enforce a minimum order total of 10 for the rule order_total_greater_than_or_equal
. Here's how to do it:
1. Create a custom validation file
β‘οΈ config/validator/ShippingMethodRule.yaml
or config/validator/ShippingMethodRule.xml
2. Register your custom validation group in the service container
β οΈ Important Notes:
The parameter name must exactly match the rule key (order_total_greater_than_or_equal
) as defined in your ShippingMethodRule
configuration.
Sylius uses this key to resolve the correct validation group when processing the rule, both in the Admin UI and API.
Be aware that other rule types like order_total_less_than_or_equal
will require separate entries.
To find the parameter you want to customize just run:
β
Result
This ensures that when the order_total_greater_than_or_equal
rule is used, the configured amount must be at least 10. If not, a validation error will be triggered.
When using custom validation messages, .
You can find all the base configurations of the groups .
If youβd like to use group sequence validation (e.g. to validate some constraints before others), .
Make sure to use [Default]
as your validation group; otherwise, your getGroupSequence()
method wonβt be triggered.