Skip to content

Commit 10810ad

Browse files
committed
Added CssColor constraint
1 parent 5f75c74 commit 10810ad

File tree

3 files changed

+198
-0
lines changed

3 files changed

+198
-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: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
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+
hex_long_with_alpha
133+
...................
134+
135+
A regular expression. Allows all values which represent a CSS color with alpha part
136+
of 8 characters (in addition of the leading ``#``) and contained in ranges: 0 to 9 and A to F (case insensitive).
137+
138+
hex_short
139+
.........
140+
141+
A simple regular expression. Allows all values which represent a CSS color
142+
of strictly 3 characters (in addition of the leading ``#``) and contained in ranges: 0 to 9 and A to F (case insensitive).
143+
144+
hex_short_with_alpha
145+
....................
146+
147+
A simple regular expression. Allows all values which represent a CSS color with alpha part
148+
of strictly 4 characters (in addition of the leading ``#``) and contained in ranges: 0 to 9 and A to F (case insensitive).
149+
150+
basic_named_colors
151+
..................
152+
153+
Accordingly to the `W3C list of basic named colors`_, it allows to use colors by their names.
154+
155+
extended_named_colors
156+
..................
157+
158+
Accordingly to the `W3C list of extended named colors`_, it allows to use colors by their names.
159+
160+
system_colors
161+
.............
162+
163+
Accordingly to the `CSS WG list of system colors`_, it allows to use colors by their names.
164+
165+
keywords
166+
........
167+
168+
Accordingly to the `CSS WG list of keywords`_, it allows to use colors by their names.
169+
170+
rgb
171+
........
172+
173+
A simple regular expression. Allows all values which represent a CSS color following th RGB notation, with or without space between values.
174+
175+
rgba
176+
........
177+
178+
A simple regular expression. Allows all values which represent a CSS color with alpha part following th RGB notation, with or without space between values.
179+
180+
hsl
181+
........
182+
183+
A simple regular expression. Allows all values which represent a CSS color following th HSL notation, with or without space between values.
184+
185+
hsla
186+
........
187+
188+
A simple regular expression. Allows all values which represent a CSS color with alpha part following th HSLA notation, with or without space between values.
189+
190+
191+
.. include:: /reference/constraints/_payload-option.rst.inc
192+
193+
.. _`W3C list of basic named colors`: https://www.w3.org/wiki/CSS/Properties/color/keywords#Basic_Colors
194+
.. _`W3C list of extended named colors`: https://www.w3.org/wiki/CSS/Properties/color/keywords#Extended_colors
195+
.. _`CSS WG list of system colors`: https://drafts.csswg.org/css-color/#css-system-colors
196+
.. _`CSS WG list of keywords`: https://drafts.csswg.org/css-color/#transparent-color

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)