SyliusCon 2025
Early Bird Deal
LogoLogo
πŸ›£οΈ RoadmapπŸ’» Sylius DemoπŸ’¬ Community Slack
  • Sylius Documentation
  • Sylius Plugins
  • Sylius Stack
  • πŸ“–Sylius 2.0 Documentation
    • Organization
      • Release Cycle
      • Backwards Compatibility Promise
      • Sylius Team
      • Sylius Roadmap
  • Getting Started with Sylius
    • Installation
    • Basic Configuration
    • Shipping & Payment
    • First Product
    • Customizing the Shop
    • Customizing Business Logic
    • Using API
    • Installing Plugins
    • Deployment
    • Summary
  • The Book
    • Introduction to Sylius
    • Installation
      • System Requirements
      • Sylius CE Installation
        • Sylius CE Installation with Docker
      • βž•Sylius Plus Installation
      • Upgrading Sylius CE
      • Upgrading Sylius Plus
    • Architecture
      • Architecture Overview
      • Architectural Drivers
      • Resource Layer
      • State Machine
      • Translations
      • E-Mails
      • Contact
      • Fixtures
      • Events
    • Configuration
      • Channels
      • Locales
      • Currencies
    • Customers
      • Customer & ShopUser
      • βž•Customer Pools
      • AdminUser
      • Addresses
        • Countries
        • Zones
        • Addresses
        • Address Book
    • Products
      • Products
      • Product Reviews
      • Product Associations
      • Attributes
      • Pricing
      • Catalog Promotions
      • Taxons
      • Inventory
      • βž•Multi-Source Inventory
      • Search
    • Carts & Orders
      • Orders
      • Cart flow
      • Taxation
      • Adjustments
      • Cart Promotions
      • Coupons
      • Payments
      • 🧩Invoices
      • Shipments
    • Support
    • Contributing
      • Contributing Code
        • Submitting a Patch
        • ⚠️Security Issues
        • Coding Standards
        • Conventions
        • Sylius License and Trademark
      • Contributing Translations
      • Key Contributors
  • The Customization Guide
    • Customizing Models
      • How to add a custom model?
      • How to add a custom translatable model?
    • Customizing Forms
      • How to add a live form for a custom model?
    • Customizing Styles
    • Customizing Validation
    • Customizing Menus
    • Customizing Templates
    • Customizing Translations
    • Customizing Flashes
    • Customizing State Machines
    • Customizing Grids
    • Customizing Fixtures
    • Customizing API
    • Customizing Serialization of API
    • Customizing Payments
      • How to integrate a Payment Gateway as a Plugin?
  • πŸ§‘β€πŸ³The Cookbook
  • How to resize images?
  • How to add one image to an entity?
  • How to add multiple images to an entity?
  • Sylius 1.X Documentation
    • πŸ““Sylius 1.x Documentation
Powered by GitBook
LogoLogo

Developer

  • Community
  • Online Course

About

  • Team

Β© 2025 Sylius. All Rights Reserved

On this page
  • How to customize validation?
  • 1. Create a custom validation file
  • 2. Register your custom validation group in the service container
  • 3. Custom validation for special cases (ShippingMethod / Promotions / Zones)

Was this helpful?

Edit on GitHub
  1. The Customization Guide

Customizing Validation

PreviousCustomizing StylesNextCustomizing Menus

Last updated 3 days ago

Was this helpful?

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


How to customize validation?

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.


1. Create a custom validation file

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:

# config/validator/ProductTranslation.yaml

Sylius\Component\Product\Model\ProductTranslation:
    properties:
        name:
            - NotBlank:
                message: sylius.product.name.not_blank
                groups: [app_product]
            - Length:
                min: 10
                minMessage: sylius.product.name.min_length
                max: 255
                maxMessage: sylius.product.name.max_length
                groups: [app_product]
<?xml version="1.0" encoding="UTF-8"?>

<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">
    <class name="Sylius\Component\Product\Model\ProductTranslation">
        <property name="name">
            <constraint name="NotBlank">
                <option name="message">sylius.product.name.not_blank</option>
                <option name="groups">app_product</option>
            </constraint>
            <constraint name="Length">
                <option name="min">10</option>
                <option name="minMessage">sylius.product.name.min_length</option>
                <option name="max">255</option>
                <option name="maxMessage">sylius.product.name.max_length</option>
                <option name="groups">app_product</option>
            </constraint>
        </property>
    </class>
</constraint-mapping>

2. Register your custom validation group in the service container

Add the following to your config/services.yaml:

parameters:
    sylius.form.type.product_translation.validation_groups: [app_product]
    sylius.form.type.product.validation_groups: [app_product] # So the Product class is aware of its translation validation

βœ… Result

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.


3. Custom validation for special cases (ShippingMethod / Promotions / Zones)

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


Example: Validating a ShippingMethodRule

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

# config/validator/ShippingMethodRule.yaml

Sylius\Component\Shipping\Model\ShippingMethodRule:
    properties:
        configuration:
            - Sylius\Bundle\CoreBundle\Validator\Constraints\ChannelCodeCollection:
                  groups: app_shipping_method_rule_order_grater_than_or_equal
                  validateAgainstAllChannels: true
                  channelAwarePropertyPath: shippingMethod
                  constraints:
                      - Collection:
                            fields:
                                amount:
                                    - range:
                                          groups: app_shipping_method_rule_order_grater_than_or_equal
                                          min: 1000
                                          max: 1000000
                  allowExtraFields: true
<?xml version="1.0" encoding="UTF-8"?>

<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">
    <class name="Sylius\Component\Shipping\Model\ShippingMethodRule">
        <property name="configuration">
            <constraint name="Sylius\Bundle\CoreBundle\Validator\Constraints\ChannelCodeCollection">
                <option name="groups">app_shipping_method_rule_order_grater_than_or_equal</option>
                <option name="validateAgainstAllChannels">true</option>
                <option name="channelAwarePropertyPath">shippingMethod</option>
                <option name="constraints">
                    <constraint name="Collection">
                        <option name="fields">
                            <value key="amount">
                                <constraint name="range">
                                    <option name="groups">app_shipping_method_rule_order_grater_than_or_equal</option>
                                    <option name="min">1000</option>
                                    <option name="max">1000000</option>
                                </constraint>
                            </value>
                        </option>
                    </constraint>
                </option>
                <option name="allowExtraFields">true</option>
            </constraint>
        </property>
    </class>
</constraint-mapping>

2. Register your custom validation group in the service container

parameters:
    sylius.shipping.shipping_method_rule.validation_groups:
        order_total_greater_than_or_equal: [app_shipping_method_rule_order_grater_than_or_equal]

⚠️ 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:

php bin/console debug:container --parameters --env=dev | grep validation_groups

βœ… 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.

🧠 Advanced Tip – Using Group Sequence Validation:

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.

he
re
learn here how to translate them
here
read more here