Documentation Home »The Oro Cookbook »How to Add custom User Validation Constraints
2.3 version

How to Add custom User Validation Constraints

The UserBundle from OroPlatform already ships a set of predefined validation constraints for the User entity. These rules are checked when a user is either being created or being modified.

You can define your own validation rules which are applied on top of the predefined rules when a User is being validated:

  • YAML
    1
    2
    3
    4
    5
    6
    # src/Acme/UserBundle/Resources/config/validation.yml
    Oro\Bundle\UserBundle\Entity\User:
        properties:
            enabled:
                - False:
                    groups: [Registration]
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    <!-- src/Acme/UserBundle/Resources/config/validation.xml -->
    <?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/constraint-mapping/constraint-mapping-1.0.xsd"
    >
        <class name="Oro\Bundle\UserBundle\Entity\User">
            <property name="password">
                <constraint name="False">
                    <option name="groups">
                        <value>Registration</value>
                    </option>
                </constraint>
            </property>
        </class>
    </constraint-mapping>
    

The example ensures that a user is not activated after being registered. The validation group associated to a constraint determines when the constraint is applied to the User entity:

Validation Group Applied when a user is created Applied when a user is modified
Registration yes no
Roles yes yes
No group yes yes
Browse maintained versions:
current1.102.02.32.4
ssossossosso