Skip to content

Commit 70bcc38

Browse files
committed
Added CssColor constraint
1 parent 5f75c74 commit 70bcc38

File tree

3 files changed

+224
-0
lines changed

3 files changed

+224
-0
lines changed

reference/constraints.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ Validation Constraints Reference
7676
constraints/NotCompromisedPassword
7777
constraints/Valid
7878
constraints/Traverse
79+
constraints/CssColor
7980

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

reference/constraints/CssColor.rst

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
CssColor
2+
=========
3+
4+
Validates that a value is a valid CSS color. The underlying value is
5+
casted to a string before being validated.
6+
7+
========== ===================================================================
8+
Applies to :ref:`property or method <validation-property-target>`
9+
Options - `groups`_
10+
- `message`_
11+
- `formats`_
12+
- `payload`_
13+
Class :class:`Symfony\\Component\\Validator\\Constraints\\CssColor`
14+
Validator :class:`Symfony\\Component\\Validator\\Constraints\\CssColorValidator`
15+
========== ===================================================================
16+
17+
Basic Usage
18+
-----------
19+
20+
.. configuration-block::
21+
22+
.. code-block:: php-annotations
23+
24+
// src/Entity/Bulb.php
25+
namespace App\Entity;
26+
27+
use Symfony\Component\Validator\Constraints as Assert;
28+
29+
class Bulb
30+
{
31+
/**
32+
* @Assert\CssColor(
33+
* message = "The color '{{ value }}' is not a valid CSS color."
34+
* )
35+
*/
36+
protected $currentColor;
37+
}
38+
39+
.. code-block:: yaml
40+
41+
# config/validator/validation.yaml
42+
App\Entity\Bulb:
43+
properties:
44+
currentColor:
45+
- CssColor:
46+
message: The color "{{ value }}" is not a valid CSS color.
47+
48+
.. code-block:: xml
49+
50+
<!-- config/validator/validation.xml -->
51+
<?xml version="1.0" encoding="UTF-8" ?>
52+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
53+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
54+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
55+
56+
<class name="App\Entity\Bulb">
57+
<property name="currentColor">
58+
<constraint name="CssColor">
59+
<option name="message">The color "{{ value }}" is not a valid CSS color.</option>
60+
</constraint>
61+
</property>
62+
</class>
63+
</constraint-mapping>
64+
65+
.. code-block:: php
66+
67+
// src/Entity/Bulb.php
68+
namespace App\Entity;
69+
70+
use Symfony\Component\Validator\Constraints as Assert;
71+
use Symfony\Component\Validator\Mapping\ClassMetadata;
72+
73+
class Bulb
74+
{
75+
public static function loadValidatorMetadata(ClassMetadata $metadata)
76+
{
77+
$metadata->addPropertyConstraint('currentColor', new Assert\CssColor([
78+
'message' => 'The color "{{ value }}" is not a valid CSS color.',
79+
]));
80+
}
81+
}
82+
83+
.. include:: /reference/constraints/_empty-values-are-valid.rst.inc
84+
85+
Options
86+
-------
87+
88+
.. include:: /reference/constraints/_groups-option.rst.inc
89+
90+
message
91+
~~~~~~~
92+
93+
**type**: ``string`` **default**: ``This value is not a valid CSS color.``
94+
95+
This message is shown if the underlying data is not a valid CSS color.
96+
97+
You can use the following parameters in this message:
98+
99+
=============== ==============================================================
100+
Parameter Description
101+
=============== ==============================================================
102+
``{{ value }}`` The current (invalid) value
103+
=============== ==============================================================
104+
105+
formats
106+
~~~~~~~
107+
108+
**type**: ``string`` | ``array``
109+
110+
This option is optional and defines the pattern the CSS color is validated against.
111+
Valid values are:
112+
113+
* ``hex_long``
114+
* ``hex_long_with_alpha``
115+
* ``hex_short``
116+
* ``hex_short_with_alpha``
117+
* ``basic_named_colors``
118+
* ``extended_named_colors``
119+
* ``system_colors``
120+
* ``keywords``
121+
* ``rgb``
122+
* ``rgba``
123+
* ``hsl``
124+
* ``hsla``
125+
126+
hex_long
127+
........
128+
129+
A regular expression. Allows all values which represent a CSS color
130+
of 6 characters (in addition of the leading ``#``) and contained in ranges: 0 to 9 and A to F (case insensitive).
131+
132+
Examples: ``#2F2F2F``, ``#2f2f2f``
133+
134+
hex_long_with_alpha
135+
...................
136+
137+
A regular expression. Allows all values which represent a CSS color with alpha part
138+
of 8 characters (in addition of the leading ``#``) and contained in ranges: 0 to 9 and A to F (case insensitive).
139+
140+
Examples: ``#2F2F2F80``, ``#2f2f2f80``
141+
142+
hex_short
143+
.........
144+
145+
A regular expression. Allows all values which represent a CSS color
146+
of strictly 3 characters (in addition of the leading ``#``) and contained in ranges: 0 to 9 and A to F (case insensitive).
147+
148+
Examples: ``#CCC``, ``#ccc``
149+
150+
hex_short_with_alpha
151+
....................
152+
153+
A regular expression. Allows all values which represent a CSS color with alpha part
154+
of strictly 4 characters (in addition of the leading ``#``) and contained in ranges: 0 to 9 and A to F (case insensitive).
155+
156+
Examples: ``#CCC8``, ``#ccc8``
157+
158+
basic_named_colors
159+
..................
160+
161+
Accordingly to the `W3C list of basic named colors`_, it allows to use colors by their names (case insensitive).
162+
163+
Examples: ``black``, ``red``, ``green``
164+
165+
extended_named_colors
166+
.....................
167+
168+
Accordingly to the `W3C list of extended named colors`_, it allows to use colors by their names (case insensitive).
169+
170+
Examples: ``aqua``, ``brown``, ``chocolate``
171+
172+
system_colors
173+
.............
174+
175+
Accordingly to the `CSS WG list of system colors`_, it allows to use colors by their names (case insensitive).
176+
177+
Examples: ``LinkText``, ``VisitedText``, ``ActiveText``, ``ButtonFace``, ``ButtonText``
178+
179+
keywords
180+
........
181+
182+
Accordingly to the `CSS WG list of keywords`_, it allows to use colors by their names (case insensitive).
183+
184+
Examples: ``transparent``, ``currentColor``
185+
186+
rgb
187+
...
188+
189+
A regular expression. Allows all values which represent a CSS color following th RGB notation, with or without space between values.
190+
191+
Examples: ``rgb(255, 255, 255)``, ``rgb(255,255,255)``
192+
193+
rgba
194+
....
195+
196+
A regular expression. Allows all values which represent a CSS color with alpha part following th RGB notation, with or without space between values.
197+
198+
Examples: ``rgba(255, 255, 255, 0.3)``, ``rgba(255,255,255,0.3)``
199+
200+
hsl
201+
...
202+
203+
A regular expression. Allows all values which represent a CSS color following th HSL notation, with or without space between values.
204+
205+
Examples: ``hsl(0, 0%, 20%)``, ``hsl(0,0%,20%)``
206+
207+
hsla
208+
....
209+
210+
A regular expression. Allows all values which represent a CSS color with alpha part following th HSLA notation, with or without space between values.
211+
212+
Examples: ``hsla(0, 0%, 20%, 0.4)``, ``hsla(0,0%,20%,0.4)``
213+
214+
.. include:: /reference/constraints/_payload-option.rst.inc
215+
216+
.. _`W3C list of basic named colors`: https://www.w3.org/wiki/CSS/Properties/color/keywords#Basic_Colors
217+
.. _`W3C list of extended named colors`: https://www.w3.org/wiki/CSS/Properties/color/keywords#Extended_colors
218+
.. _`CSS WG list of system colors`: https://drafts.csswg.org/css-color/#css-system-colors
219+
.. _`CSS WG list of keywords`: https://drafts.csswg.org/css-color/#transparent-color
220+
221+
222+

reference/constraints/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ String Constraints
2727
* :doc:`Ulid </reference/constraints/Ulid>`
2828
* :doc:`UserPassword </reference/constraints/UserPassword>`
2929
* :doc:`NotCompromisedPassword </reference/constraints/NotCompromisedPassword>`
30+
* :doc:`CssColor </reference/constraints/CssColor>`
3031

3132
Comparison Constraints
3233
~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)