-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Documented htmlPattern option #2864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ Validates that a value matches a regular expression. | |
| Applies to | :ref:`property or method<validation-property-target>` | | ||
+----------------+-----------------------------------------------------------------------+ | ||
| Options | - `pattern`_ | | ||
| | - `htmlPattern`_ | | ||
| | - `match`_ | | ||
| | - `message`_ | | ||
+----------------+-----------------------------------------------------------------------+ | ||
|
@@ -161,6 +162,87 @@ does *not* match this regular expression (via the :phpfunction:`preg_match` PHP | |
However, if `match`_ is set to false, then validation will fail if the input | ||
string *does* match this pattern. | ||
|
||
htmlPattern | ||
~~~~~~~~~~~ | ||
|
||
.. versionadded:: 2.1 | ||
The ``htmlPattern`` option was added in Symfony 2.1 | ||
|
||
**type**: ``string|Boolean`` **default**: null | ||
|
||
This option specifies the pattern to use in the html5 ``pattern`` attribute. | ||
By default, the constraint will convert the pattern given in the ``pattern`` | ||
option into a html5 compatible pattern. This means that the delimeters are | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
removed (e.g. ``/[a-z]+/`` becomes ``[a-z]+``). | ||
|
||
However, their are some other incompatibilities between both patterns which | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there |
||
cannot be fixed by the constraint. For instance, the html5 pattern attribute | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. HTML5? |
||
does not support flags. If you have a pattern like ``/[a-z]+/i`` you need to | ||
specify the html5 compatible pattern in the ``htmlPattern`` option: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
# src/Acme/BlogBundle/Resources/config/validation.yml | ||
Acme\BlogBundle\Entity\Author: | ||
properties: | ||
name: | ||
- Regex: | ||
pattern: "/^[a-z]+$/i" | ||
htmlPattern: "^[a-zA-Z]+$" | ||
|
||
.. code-block:: php-annotations | ||
|
||
// src/Acme/BlogBundle/Entity/Author.php | ||
namespace Acme\BlogBundle\Entity; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
class Author | ||
{ | ||
/** | ||
* @Assert\Regex({ | ||
* pattern = "/^[a-z]+$/i", | ||
* htmlPattern = "^[a-zA-Z]+$" | ||
* }) | ||
*/ | ||
protected $name; | ||
} | ||
|
||
.. code-block:: xml | ||
|
||
<!-- src/Acme/BlogBundle/Resources/config/validation.xml --> | ||
<class name="Acme\BlogBundle\Entity\Author"> | ||
<property name="name"> | ||
<constraint name="Regex"> | ||
<option name="pattern">/^[a-z]+$/i</option> | ||
<option name="htmlPattern">^[a-zA-Z]+$</option> | ||
</constraint> | ||
</property> | ||
</class> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. <!-- src/Acme/BlogBundle/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="Acme\BlogBundle\Entity\Author">
<property name="name">
<constraint name="Regex">
<option name="pattern">/^[a-z]+$/i</option>
<option name="htmlPattern">^[a-zA-Z]+$</option>
</constraint>
</property>
</class>
</constraint-mapping> |
||
|
||
.. code-block:: php | ||
|
||
// src/Acme/BlogBundle/Entity/Author.php | ||
namespace Acme\BlogBundle\Entity; | ||
|
||
use Symfony\Component\Validator\Mapping\ClassMetadata; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
class Author | ||
{ | ||
public static function loadValidatorMetadata(ClassMetadata $metadata) | ||
{ | ||
$metadata->addPropertyConstraint('name', new Assert\Regex(array( | ||
'pattern' => '/^[a-z]+$/i', | ||
'htmlPattern' => '^[a-zA-Z]+$', | ||
))); | ||
} | ||
} | ||
|
||
Setting ``htmlPattern`` to false will disable client side validation. | ||
|
||
match | ||
~~~~~ | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HTML5?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what exactly do you mean?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer the uppercase notation for HTML5.