From f2ed58b18378464345b3cf01232b4214be5dbe0d Mon Sep 17 00:00:00 2001 From: WouterJ Date: Tue, 30 Jul 2013 13:47:30 +0200 Subject: [PATCH] Documented htmlPattern option --- reference/constraints/Regex.rst | 82 +++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/reference/constraints/Regex.rst b/reference/constraints/Regex.rst index 426bd72f196..a5d69baef9f 100644 --- a/reference/constraints/Regex.rst +++ b/reference/constraints/Regex.rst @@ -7,6 +7,7 @@ Validates that a value matches a regular expression. | Applies to | :ref:`property or method` | +----------------+-----------------------------------------------------------------------+ | 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 +removed (e.g. ``/[a-z]+/`` becomes ``[a-z]+``). + +However, their are some other incompatibilities between both patterns which +cannot be fixed by the constraint. For instance, the html5 pattern attribute +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: + +.. 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 + + + + + + + + + + + + .. 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 ~~~~~