Skip to content

Commit 07e38b7

Browse files
committed
[Validator] Some tweaks to CssColor constraint
1 parent 0710b02 commit 07e38b7

File tree

1 file changed

+89
-45
lines changed

1 file changed

+89
-45
lines changed

reference/constraints/CssColor.rst

Lines changed: 89 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
CssColor
22
=========
33

4+
.. versionadded:: 5.4
5+
6+
The ``CssColor`` constraint was introduced in Symfony 5.4.
7+
48
Validates that a value is a valid CSS color. The underlying value is
59
casted to a string before being validated.
610

@@ -17,6 +21,12 @@ Validator :class:`Symfony\\Component\\Validator\\Constraints\\CssColorValidato
1721
Basic Usage
1822
-----------
1923

24+
In the following example, the ``$defaultColor`` value must be a CSS color
25+
defined in any of the valid CSS formats (e.g. ``red``, ``#369``,
26+
``hsla(0, 0%, 20%, 0.4)``); the ``$accentColor`` must be a CSS color defined in
27+
hexadecimal format; and ``$currentColor`` must be a CSS color defined as any of
28+
the named CSS colors:
29+
2030
.. configuration-block::
2131

2232
.. code-block:: php-annotations
@@ -28,18 +38,26 @@ Basic Usage
2838
2939
class Bulb
3040
{
41+
/**
42+
* @Assert\CssColor
43+
*/
44+
protected $defaultColor;
45+
3146
/**
3247
* @Assert\CssColor(
33-
* formats = { Assert\CssColor::HEX_LONG }
34-
* message = "The color '{{ value }}' is not a valid CSS color."
48+
* formats = Assert\CssColor::HEX_LONG,
49+
* message = "The accent color must be a 6-character hexadecimal color."
3550
* )
3651
*/
37-
protected $defaultColor;
52+
protected $accentColor;
3853
3954
/**
4055
* @Assert\CssColor(
41-
* formats = Assert\CssColor::BASIC_NAMED_COLORS
42-
* message = "The color '{{ value }}' is not a valid CSS color."
56+
* formats = {
57+
* Assert\CssColor::BASIC_NAMED_COLORS,
58+
* Assert\CssColor::EXTENDED_NAMED_COLORS
59+
* },
60+
* message = "The color '{{ value }}' is not a valid CSS color name."
4361
* )
4462
*/
4563
protected $currentColor;
@@ -54,15 +72,18 @@ Basic Usage
5472
5573
class Bulb
5674
{
75+
#[Assert\CssColor]
76+
protected $defaultColor;
77+
5778
#[Assert\CssColor(
58-
formats: [Assert\CssColor::HEX_LONG]
59-
message: 'The color '{{ value }}' is not a valid CSS color.',
79+
formats: Assert\CssColor::HEX_LONG,
80+
message: 'The accent color must be a 6-character hexadecimal color.',
6081
)]
61-
protected $defaultColor;
82+
protected $accentColor;
6283
6384
#[Assert\CssColor(
64-
formats: Assert\CssColor::BASIC_NAMED_COLORS
65-
message: 'The color '{{ value }}' is not a valid CSS color.',
85+
formats: [Assert\CssColor::BASIC_NAMED_COLORS, Assert\CssColor::EXTENDED_NAMED_COLORS],
86+
message: 'The color '{{ value }}' is not a valid CSS color name.',
6687
)]
6788
protected $currentColor;
6889
}
@@ -73,13 +94,17 @@ Basic Usage
7394
App\Entity\Bulb:
7495
properties:
7596
defaultColor:
97+
- CssColor: ~
98+
accentColor:
7699
- CssColor:
77-
formats: [ !php/const Symfony\Component\Validator\Constraints\CssColor::HEX_LONG ]
78-
message: The color "{{ value }}" is not a valid CSS color.
100+
formats: !php/const Symfony\Component\Validator\Constraints\CssColor::HEX_LONG
101+
message: The accent color must be a 6-character hexadecimal color.
79102
currentColor:
80103
- CssColor:
81-
formats: !php/const Symfony\Component\Validator\Constraints\CssColor::BASIC_NAMED_COLORS
82-
message: The color "{{ value }}" is not a valid CSS color.
104+
formats:
105+
- !php/const Symfony\Component\Validator\Constraints\CssColor::BASIC_NAMED_COLORS
106+
- !php/const Symfony\Component\Validator\Constraints\CssColor::EXTENDED_NAMED_COLORS
107+
message: The color "{{ value }}" is not a valid CSS color name.
83108
84109
.. code-block:: xml
85110
@@ -90,18 +115,22 @@ Basic Usage
90115
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
91116
92117
<class name="App\Entity\Bulb">
93-
<property name="currentColor">
118+
<property name="defaultColor">
119+
<constraint name="CssColor"/>
120+
</property>
121+
<property name="accentColor">
94122
<constraint name="CssColor">
95-
<option name="formats">
96-
<value>hex_long</value>
97-
</option>
98-
<option name="message">The color "{{ value }}" is not a valid CSS color.</option>
123+
<option name="formats">hex_long</option>
124+
<option name="message">The accent color must be a 6-character hexadecimal color.</option>
99125
</constraint>
100126
</property>
101-
<property name="defaultColor">
127+
<property name="currentColor">
102128
<constraint name="CssColor">
103-
<option name="formats">basic_named_colors</option>
104-
<option name="message">The color "{{ value }}" is not a valid CSS color.</option>
129+
<option name="formats">
130+
<value>basic_named_colors</value>
131+
<value>extended_named_colors</value>
132+
</option>
133+
<option name="message">The color "{{ value }}" is not a valid CSS color name.</option>
105134
</constraint>
106135
</property>
107136
</class>
@@ -119,14 +148,16 @@ Basic Usage
119148
{
120149
public static function loadValidatorMetadata(ClassMetadata $metadata)
121150
{
122-
$metadata->addPropertyConstraint('defaultColor', new Assert\CssColor([
123-
'formats' => [Assert\CssColor::HEX_LONG],
124-
'message' => 'The color "{{ value }}" is not a valid CSS color.',
151+
$metadata->addPropertyConstraint('defaultColor', new Assert\CssColor());
152+
153+
$metadata->addPropertyConstraint('accentColor', new Assert\CssColor([
154+
'formats' => Assert\CssColor::HEX_LONG,
155+
'message' => 'The accent color must be a 6-character hexadecimal color.',
125156
]));
126157
127158
$metadata->addPropertyConstraint('currentColor', new Assert\CssColor([
128-
'formats' => Assert\CssColor::BASIC_NAMED_COLORS,
129-
'message' => 'The color "{{ value }}" is not a valid CSS color.',
159+
'formats' => [Assert\CssColor::BASIC_NAMED_COLORS, Assert\CssColor::EXTENDED_NAMED_COLORS],
160+
'message' => 'The color "{{ value }}" is not a valid CSS color name.',
130161
]));
131162
}
132163
}
@@ -158,8 +189,10 @@ formats
158189

159190
**type**: ``string`` | ``array``
160191

161-
This option is optional and defines the pattern the CSS color is validated against.
162-
Valid values are:
192+
By default, this constraint considers valid any of the many ways of defining
193+
CSS colors. Use the ``formats`` option to restrict which CSS formats are allowed.
194+
These are the available formats (which are also defined as PHP constants; e.g.
195+
``Assert\CssColor::HEX_LONG``):
163196

164197
* ``hex_long``
165198
* ``hex_long_with_alpha``
@@ -177,88 +210,99 @@ Valid values are:
177210
hex_long
178211
........
179212

180-
A regular expression. Allows all values which represent a CSS color
181-
of 6 characters (in addition of the leading ``#``) and contained in ranges: 0 to 9 and A to F (case insensitive).
213+
A regular expression. Allows all values which represent a CSS color of 6
214+
characters (in addition of the leading ``#``) and contained in ranges: ``0`` to
215+
``9`` and ``A`` to ``F`` (case insensitive).
182216

183217
Examples: ``#2F2F2F``, ``#2f2f2f``
184218

185219
hex_long_with_alpha
186220
...................
187221

188-
A regular expression. Allows all values which represent a CSS color with alpha part
189-
of 8 characters (in addition of the leading ``#``) and contained in ranges: 0 to 9 and A to F (case insensitive).
222+
A regular expression. Allows all values which represent a CSS color with alpha
223+
part of 8 characters (in addition of the leading ``#``) and contained in
224+
ranges: ``0`` to ``9`` and ``A`` to ``F`` (case insensitive).
190225

191226
Examples: ``#2F2F2F80``, ``#2f2f2f80``
192227

193228
hex_short
194229
.........
195230

196-
A regular expression. Allows all values which represent a CSS color
197-
of strictly 3 characters (in addition of the leading ``#``) and contained in ranges: 0 to 9 and A to F (case insensitive).
231+
A regular expression. Allows all values which represent a CSS color of strictly
232+
3 characters (in addition of the leading ``#``) and contained in ranges: ``0``
233+
to ``9`` and ``A`` to ``F`` (case insensitive).
198234

199235
Examples: ``#CCC``, ``#ccc``
200236

201237
hex_short_with_alpha
202238
....................
203239

204-
A regular expression. Allows all values which represent a CSS color with alpha part
205-
of strictly 4 characters (in addition of the leading ``#``) and contained in ranges: 0 to 9 and A to F (case insensitive).
240+
A regular expression. Allows all values which represent a CSS color with alpha
241+
part of strictly 4 characters (in addition of the leading ``#``) and contained
242+
in ranges: ``0`` to ``9`` and ``A`` to ``F`` (case insensitive).
206243

207244
Examples: ``#CCC8``, ``#ccc8``
208245

209246
basic_named_colors
210247
..................
211248

212-
Accordingly to the `W3C list of basic named colors`_, it allows to use colors by their names (case insensitive).
249+
Any of the valid color names defined in the `W3C list of basic named colors`_
250+
(case insensitive).
213251

214252
Examples: ``black``, ``red``, ``green``
215253

216254
extended_named_colors
217255
.....................
218256

219-
Accordingly to the `W3C list of extended named colors`_, it allows to use colors by their names (case insensitive).
257+
Any of the valid color names defined in the `W3C list of extended named colors`_
258+
(case insensitive).
220259

221260
Examples: ``aqua``, ``brown``, ``chocolate``
222261

223262
system_colors
224263
.............
225264

226-
Accordingly to the `CSS WG list of system colors`_, it allows to use colors by their names (case insensitive).
265+
Any of the valid color names defined in the `CSS WG list of system colors`_
266+
(case insensitive).
227267

228268
Examples: ``LinkText``, ``VisitedText``, ``ActiveText``, ``ButtonFace``, ``ButtonText``
229269

230270
keywords
231271
........
232272

233-
Accordingly to the `CSS WG list of keywords`_, it allows to use colors by their names (case insensitive).
273+
Any of the valid keywords defined in the `CSS WG list of keywords`_ (case insensitive).
234274

235275
Examples: ``transparent``, ``currentColor``
236276

237277
rgb
238278
...
239279

240-
A regular expression. Allows all values which represent a CSS color following th RGB notation, with or without space between values.
280+
A regular expression. Allows all values which represent a CSS color following
281+
the RGB notation, with or without space between values.
241282

242283
Examples: ``rgb(255, 255, 255)``, ``rgb(255,255,255)``
243284

244285
rgba
245286
....
246287

247-
A regular expression. Allows all values which represent a CSS color with alpha part following th RGB notation, with or without space between values.
288+
A regular expression. Allows all values which represent a CSS color with alpha
289+
part following the RGB notation, with or without space between values.
248290

249291
Examples: ``rgba(255, 255, 255, 0.3)``, ``rgba(255,255,255,0.3)``
250292

251293
hsl
252294
...
253295

254-
A regular expression. Allows all values which represent a CSS color following th HSL notation, with or without space between values.
296+
A regular expression. Allows all values which represent a CSS color following
297+
the HSL notation, with or without space between values.
255298

256299
Examples: ``hsl(0, 0%, 20%)``, ``hsl(0,0%,20%)``
257300

258301
hsla
259302
....
260303

261-
A regular expression. Allows all values which represent a CSS color with alpha part following th HSLA notation, with or without space between values.
304+
A regular expression. Allows all values which represent a CSS color with alpha
305+
part following the HSLA notation, with or without space between values.
262306

263307
Examples: ``hsla(0, 0%, 20%, 0.4)``, ``hsla(0,0%,20%,0.4)``
264308

0 commit comments

Comments
 (0)