How to Customize Validation

Overriding existing validation

To override existing validation rules, you will have to copy the existing validation to your own bundle.

For example, to allow spaces in option codes, create the following file:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# /src/Acme/Bundle/CatalogBundle/Resources/config/validation/attribute.yml
Pim\Bundle\CatalogBundle\Entity\Attribute:
    constraints:
        - Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: code
        - Pim\Bundle\CatalogBundle\Validator\Constraints\SingleIdentifierAttribute: ~
        - Pim\Bundle\CatalogBundle\Validator\Constraints\Immutable:
            properties:
                - code
                - attributeType
                - scopable
                - localizable
                - metricFamily
                - unique
        - Pim\Bundle\CatalogBundle\Validator\Constraints\ValidMetric:
            groups:
                - pim_catalog_metric
        - Pim\Bundle\CatalogBundle\Validator\Constraints\ValidDateRange:
            groups:
                - pim_catalog_date
        - Pim\Bundle\CatalogBundle\Validator\Constraints\ValidNumberRange:
            groups:
                - pim_catalog_number
                - pim_catalog_price_collection
                - pim_catalog_metric
    properties:
        code:
            - NotBlank: ~
            - Regex:
                pattern: /^[a-zA-Z0-9_]+$/
                message: Attribute code may contain only letters, numbers and underscores
            - Length:
                max: 255
            - Regex:
                pattern: /^(?!(id|associationTypes|categories|categoryId|completeness|enabled|family|groups|associations|products|scope|treeId|values|(.)*_(products|groups))$)/
                message: This code is not available
        localizable:
            - Type: bool
            - 'False':
                message: An unique attribute can not be localizable
                groups: [unique]
        scopable:
            - NotNull: ~
            - 'False':
                message: An unique attribute can not be scopable
                groups: [unique]
        useableAsGridFilter:
            - Type: bool
            - 'False':
                message: This attribute type can't be used as a grid filter
                groups:
                    - pim_catalog_file
                    - pim_catalog_image
        wysiwygEnabled:
            - Type: bool
        decimalsAllowed:
            - Type: bool
        negativeAllowed:
            - Type: bool
        maxCharacters:
            - Type: numeric
            - Pim\Bundle\CatalogBundle\Validator\Constraints\NotDecimal: {}
            - GreaterThanOrEqual:
                value: 0
            - Range:
                max: 255
                groups: [pim_catalog_text, pim_catalog_identifier]
            - Range:
                max: 65535
                groups: [pim_catalog_textarea]
        sortOrder:
            - Type: numeric
            - GreaterThanOrEqual:
                value: 0
        required:
            - Type: bool
            - 'True':
                message: This attribute type must be required
                groups:
                    - pim_catalog_identifier
            - 'False':
                message: This attribute type can't be required
                groups:
                    - pim_catalog_text
                    - pim_catalog_textarea
                    - pim_catalog_number
                    - pim_catalog_price_collection
                    - pim_catalog_multiselect
                    - pim_catalog_simpleselect
                    - pim_catalog_file
                    - pim_catalog_image
                    - pim_catalog_boolean
                    - pim_catalog_date
                    - pim_catalog_metric
        unique:
            - Type: bool
            - 'False':
                message: This attribute type can't have unique value
                groups:
                    - pim_catalog_textarea
                    - pim_catalog_price_collection
                    - pim_catalog_multiselect
                    - pim_catalog_simpleselect
                    - pim_catalog_image
                    - pim_catalog_file
                    - pim_catalog_metric
                    - pim_catalog_boolean
        maxFileSize:
            - Range:
                min: 0.01
                max: 9999.99
                groups: [pim_catalog_file, pim_catalog_image]
            - Type:
                type: numeric
                groups: [pim_catalog_file, pim_catalog_image]
        dateMin:
            - DateTime:
                groups: [pim_catalog_date]
        dateMax:
            - DateTime:
                groups: [pim_catalog_date]
        validationRule:
            - Choice:
                choices: [null, url, email, regexp]
                groups: [pim_catalog_text]
        validationRegexp:
            - Blank:
                groups:
                    - pim_catalog_textarea
                    - pim_catalog_multiselect
                    - pim_catalog_simpleselect
                    - pim_catalog_price_collection
                    - pim_catalog_number
                    - pim_catalog_boolean
                    - pim_catalog_date
                    - pim_catalog_file
                    - pim_catalog_image
                    - pim_catalog_metric
            - Pim\Bundle\CatalogBundle\Validator\Constraints\ValidRegex:
                groups:
                    - pim_catalog_text
                    - pim_catalog_identifier
        allowedExtensions:
            - NotNull:
                groups: [pim_catalog_file, pim_catalog_image]
                message: This value should not be blank
        translations:
            - Valid: ~
        group:
            - NotBlank: ~
            - Valid: ~

Pim\Bundle\CatalogBundle\Entity\AttributeTranslation:
    properties:
        label:
            - Length:
                max: 100

Pim\Bundle\CatalogBundle\Entity\AttributeOption:
    constraints:
        - Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
            - code
            - attribute
    properties:
        code:
            - NotBlank: ~
            - Regex:
                pattern: /^[a-zA-Z0-9_]+$/
                message: Option code may contain only letters, numbers and underscores
            - Length:
                max: 255
        attribute:
            - Valid: ~

As you can see, all the validation rules must be copied back to your file.

Adding new validation rules

To add new validation rules instead of replacing existing ones, simply create a new yml file in the config/validation directory of your bundle. The name of the file must be different from already existing validation configuration files.