Skip to content

Commit d301bfd

Browse files
committed
Added HexaColor constraint
1 parent bfba8a6 commit d301bfd

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

reference/constraints.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Validation Constraints Reference
6969
constraints/NotCompromisedPassword
7070
constraints/Valid
7171
constraints/Traverse
72+
constraints/HexaColor
7273

7374
The Validator is designed to validate objects against *constraints*.
7475
In real life, a constraint could be: "The cake must not be burned". In

reference/constraints/HexaColor.rst

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
HexaColor
2+
=========
3+
4+
Validates that a value is a valid email address. The underlying value is
5+
cast to a string before being validated.
6+
7+
========== ===================================================================
8+
Applies to :ref:`property or method <validation-property-target>`
9+
Options - `groups`_
10+
- `message`_
11+
- `mode`_
12+
- `normalizer`_
13+
- `payload`_
14+
Class :class:`Symfony\\Component\\Validator\\Constraints\\HexaColor`
15+
Validator :class:`Symfony\\Component\\Validator\\Constraints\\HexaColorValidator`
16+
========== ===================================================================
17+
18+
Basic Usage
19+
-----------
20+
21+
.. configuration-block::
22+
23+
.. code-block:: php-annotations
24+
25+
// src/Entity/Bulb.php
26+
namespace App\Entity;
27+
28+
use Symfony\Component\Validator\Constraints as Assert;
29+
30+
class Bulb
31+
{
32+
/**
33+
* @Assert\HexaColor(
34+
* message = "The color '{{ value }}' is not a valid hexadecimal color."
35+
* )
36+
*/
37+
protected $currentColor;
38+
}
39+
40+
.. code-block:: yaml
41+
42+
# config/validator/validation.yaml
43+
App\Entity\Bulb:
44+
properties:
45+
currentColor:
46+
- HexaColor:
47+
message: The color "{{ value }}" is not a valid hexadecimal color.
48+
49+
.. code-block:: xml
50+
51+
<!-- config/validator/validation.xml -->
52+
<?xml version="1.0" encoding="UTF-8" ?>
53+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
54+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
55+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
56+
57+
<class name="App\Entity\Bulb">
58+
<property name="currentColor">
59+
<constraint name="HexaColorZ">
60+
<option name="message">The color "{{ value }}" is not a valid hexadecimal color.</option>
61+
</constraint>
62+
</property>
63+
</class>
64+
</constraint-mapping>
65+
66+
.. code-block:: php
67+
68+
// src/Entity/Bulb.php
69+
namespace App\Entity;
70+
71+
use Symfony\Component\Validator\Constraints as Assert;
72+
use Symfony\Component\Validator\Mapping\ClassMetadata;
73+
74+
class Bulb
75+
{
76+
public static function loadValidatorMetadata(ClassMetadata $metadata)
77+
{
78+
$metadata->addPropertyConstraint('currentColor', new Assert\HexaColor([
79+
'message' => 'The color "{{ value }}" is not a valid hexadecimal color.',
80+
]));
81+
}
82+
}
83+
84+
.. include:: /reference/constraints/_empty-values-are-valid.rst.inc
85+
86+
Options
87+
-------
88+
89+
.. include:: /reference/constraints/_groups-option.rst.inc
90+
91+
message
92+
~~~~~~~
93+
94+
**type**: ``string`` **default**: ``This value is not a valid hexadecimal color.``
95+
96+
This message is shown if the underlying data is not a valid hexadecimal color.
97+
98+
You can use the following parameters in this message:
99+
100+
=============== ==============================================================
101+
Parameter Description
102+
=============== ==============================================================
103+
``{{ value }}`` The current (invalid) value
104+
=============== ==============================================================
105+
106+
mode
107+
~~~~
108+
109+
**type**: ``string`` **default**: ``loose``
110+
111+
This option is optional and defines the pattern the hexadecimal color is validated against.
112+
Valid values are:
113+
114+
* ``both``
115+
* ``short``
116+
* ``html5``
117+
118+
both
119+
.....
120+
121+
A regular expression. Allows all values which represent a hexadecimal color
122+
of 3 or 6 characters (in addition of the leading ``#``) and contained in ranges: 0 to 9 and A to F (case insensitive).
123+
124+
short
125+
......
126+
127+
A simple regular expression. Allows all values which represent a hexadecimal color
128+
of strictly 3 characters (in addition of the leading ``#``) and contained in ranges: 0 to 9 and A to F (case insensitive).
129+
130+
html5
131+
.....
132+
133+
As well as the HTML5 color input, this mode allows all values of strictly 6 characters (in addition of the leading ``#``) and contained in ranges: 0 to 9 and A to F (case insensitive).
134+
135+
.. include:: /reference/constraints/_normalizer-option.rst.inc
136+
137+
.. include:: /reference/constraints/_payload-option.rst.inc

reference/constraints/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ String Constraints
2424
* :doc:`Uuid</reference/constraints/Uuid>`
2525
* :doc:`UserPassword </reference/constraints/UserPassword>`
2626
* :doc:`NotCompromisedPassword </reference/constraints/NotCompromisedPassword>`
27+
* :doc:`HexaColor </reference/constraints/HexaColor>`
2728

2829
Comparison Constraints
2930
~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)