Skip to content

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

Merged
merged 1 commit into from
Aug 17, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions reference/constraints/Regex.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`_ |
+----------------+-----------------------------------------------------------------------+
Expand Down Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HTML5?

Copy link
Member Author

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?

Copy link
Member

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.

By default, the constraint will convert the pattern given in the ``pattern``
option into a html5 compatible pattern. This means that the delimeters are
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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:
Copy link
Member

Choose a reason for hiding this comment

The 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>
Copy link
Member

Choose a reason for hiding this comment

The 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
~~~~~

Expand Down