Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbd0dc8b

Browse files
committed
Merge pull request#2864 from WouterJ/issue_2833
Documented htmlPattern option
2 parents33c9b98 +f2ed58b commitbd0dc8b

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

‎reference/constraints/Regex.rst

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Validates that a value matches a regular expression.
77
| Applies to|:ref:`property or method<validation-property-target>`|
88
+----------------+-----------------------------------------------------------------------+
99
| Options| - `pattern`_|
10+
|| - `htmlPattern`_|
1011
|| - `match`_|
1112
|| - `message`_|
1213
+----------------+-----------------------------------------------------------------------+
@@ -161,6 +162,87 @@ does *not* match this regular expression (via the :phpfunction:`preg_match` PHP
161162
However, if `match`_ is set to false, then validation will fail if the input
162163
string *does* match this pattern.
163164

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+
<classname="Acme\BlogBundle\Entity\Author">
217+
<propertyname="name">
218+
<constraintname="Regex">
219+
<optionname="pattern">/^[a-z]+$/i</option>
220+
<optionname="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+
164246
match
165247
~~~~~
166248

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp