@@ -7,6 +7,7 @@ Validates that a value matches a regular expression.
7
7
| Applies to | :ref: `property or method<validation-property-target> ` |
8
8
+----------------+-----------------------------------------------------------------------+
9
9
| Options | - `pattern `_ |
10
+ | | - `htmlPattern `_ |
10
11
| | - `match `_ |
11
12
| | - `message `_ |
12
13
+----------------+-----------------------------------------------------------------------+
@@ -161,6 +162,87 @@ does *not* match this regular expression (via the :phpfunction:`preg_match` PHP
161
162
However, if `match `_ is set to false, then validation will fail if the input
162
163
string *does * match this pattern.
163
164
165
+ htmlPattern
166
+ ~~~~~~~~~~~
167
+
168
+ .. versionadded :: 2.1
169
+ The ``htmlPattern `` option was added in Symfony 2.1
170
+
171
+ **type **: ``string|Boolean `` **default **: null
172
+
173
+ This option specifies the pattern to use in the html5 ``pattern `` attribute.
174
+ By default, the constraint will convert the pattern given in the ``pattern ``
175
+ option into a html5 compatible pattern. This means that the delimeters are
176
+ removed (e.g. ``/[a-z]+/ `` becomes ``[a-z]+ ``).
177
+
178
+ However, their are some other incompatibilities between both patterns which
179
+ cannot be fixed by the constraint. For instance, the html5 pattern attribute
180
+ does not support flags. If you have a pattern like ``/[a-z]+/i `` you need to
181
+ specify the html5 compatible pattern in the ``htmlPattern `` option:
182
+
183
+ .. configuration-block ::
184
+
185
+ .. code-block :: yaml
186
+
187
+ # src/Acme/BlogBundle/Resources/config/validation.yml
188
+ Acme\BlogBundle\Entity\Author :
189
+ properties :
190
+ name :
191
+ - Regex :
192
+ pattern : " /^[a-z]+$/i"
193
+ htmlPattern : " ^[a-zA-Z]+$"
194
+
195
+ .. code-block :: php-annotations
196
+
197
+ // src/Acme/BlogBundle/Entity/Author.php
198
+ namespace Acme\BlogBundle\Entity;
199
+
200
+ use Symfony\Component\Validator\Constraints as Assert;
201
+
202
+ class Author
203
+ {
204
+ /**
205
+ * @Assert\Regex({
206
+ * pattern = "/^[a-z]+$/i",
207
+ * htmlPattern = "^[a-zA-Z]+$"
208
+ * })
209
+ */
210
+ protected $name;
211
+ }
212
+
213
+ .. code-block :: xml
214
+
215
+ <!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
216
+ <class name =" Acme\BlogBundle\Entity\Author" >
217
+ <property name =" name" >
218
+ <constraint name =" Regex" >
219
+ <option name =" pattern" >/^[a-z]+$/i</option >
220
+ <option name =" htmlPattern" >^[a-zA-Z]+$</option >
221
+ </constraint >
222
+ </property >
223
+ </class >
224
+
225
+ .. code-block :: php
226
+
227
+ // src/Acme/BlogBundle/Entity/Author.php
228
+ namespace Acme\BlogBundle\Entity;
229
+
230
+ use Symfony\Component\Validator\Mapping\ClassMetadata;
231
+ use Symfony\Component\Validator\Constraints as Assert;
232
+
233
+ class Author
234
+ {
235
+ public static function loadValidatorMetadata(ClassMetadata $metadata)
236
+ {
237
+ $metadata->addPropertyConstraint('name', new Assert\Regex(array(
238
+ 'pattern' => '/^[a-z]+$/i',
239
+ 'htmlPattern' => '^[a-zA-Z]+$',
240
+ )));
241
+ }
242
+ }
243
+
244
+ Setting ``htmlPattern `` to false will disable client side validation.
245
+
164
246
match
165
247
~~~~~
166
248
0 commit comments