1
1
CssColor
2
2
=========
3
3
4
+ .. versionadded :: 5.4
5
+
6
+ The ``CssColor `` constraint was introduced in Symfony 5.4.
7
+
4
8
Validates that a value is a valid CSS color. The underlying value is
5
9
casted to a string before being validated.
6
10
@@ -17,6 +21,12 @@ Validator :class:`Symfony\\Component\\Validator\\Constraints\\CssColorValidato
17
21
Basic Usage
18
22
-----------
19
23
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
+
20
30
.. configuration-block ::
21
31
22
32
.. code-block :: php-annotations
@@ -28,18 +38,26 @@ Basic Usage
28
38
29
39
class Bulb
30
40
{
41
+ /**
42
+ * @Assert\CssColor
43
+ */
44
+ protected $defaultColor;
45
+
31
46
/**
32
47
* @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."
35
50
* )
36
51
*/
37
- protected $defaultColor ;
52
+ protected $accentColor ;
38
53
39
54
/**
40
55
* @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."
43
61
* )
44
62
*/
45
63
protected $currentColor;
@@ -54,15 +72,18 @@ Basic Usage
54
72
55
73
class Bulb
56
74
{
75
+ #[Assert\CssColor]
76
+ protected $defaultColor;
77
+
57
78
#[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.',
60
81
)]
61
- protected $defaultColor ;
82
+ protected $accentColor ;
62
83
63
84
#[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 .',
66
87
)]
67
88
protected $currentColor;
68
89
}
@@ -73,13 +94,17 @@ Basic Usage
73
94
App\Entity\Bulb :
74
95
properties :
75
96
defaultColor :
97
+ - CssColor : ~
98
+ accentColor :
76
99
- 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.
79
102
currentColor :
80
103
- 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.
83
108
84
109
.. code-block :: xml
85
110
@@ -90,18 +115,22 @@ Basic Usage
90
115
xsi : schemaLocation =" http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd" >
91
116
92
117
<class name =" App\Entity\Bulb" >
93
- <property name =" currentColor" >
118
+ <property name =" defaultColor" >
119
+ <constraint name =" CssColor" />
120
+ </property >
121
+ <property name =" accentColor" >
94
122
<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 >
99
125
</constraint >
100
126
</property >
101
- <property name =" defaultColor " >
127
+ <property name =" currentColor " >
102
128
<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 >
105
134
</constraint >
106
135
</property >
107
136
</class >
@@ -119,14 +148,16 @@ Basic Usage
119
148
{
120
149
public static function loadValidatorMetadata(ClassMetadata $metadata)
121
150
{
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.',
125
156
]));
126
157
127
158
$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 .',
130
161
]));
131
162
}
132
163
}
@@ -158,8 +189,10 @@ formats
158
189
159
190
**type **: ``string `` | ``array ``
160
191
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 ``):
163
196
164
197
* ``hex_long ``
165
198
* ``hex_long_with_alpha ``
@@ -177,88 +210,99 @@ Valid values are:
177
210
hex_long
178
211
........
179
212
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).
182
216
183
217
Examples: ``#2F2F2F ``, ``#2f2f2f ``
184
218
185
219
hex_long_with_alpha
186
220
...................
187
221
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).
190
225
191
226
Examples: ``#2F2F2F80 ``, ``#2f2f2f80 ``
192
227
193
228
hex_short
194
229
.........
195
230
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).
198
234
199
235
Examples: ``#CCC ``, ``#ccc ``
200
236
201
237
hex_short_with_alpha
202
238
....................
203
239
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).
206
243
207
244
Examples: ``#CCC8 ``, ``#ccc8 ``
208
245
209
246
basic_named_colors
210
247
..................
211
248
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).
213
251
214
252
Examples: ``black ``, ``red ``, ``green ``
215
253
216
254
extended_named_colors
217
255
.....................
218
256
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).
220
259
221
260
Examples: ``aqua ``, ``brown ``, ``chocolate ``
222
261
223
262
system_colors
224
263
.............
225
264
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).
227
267
228
268
Examples: ``LinkText ``, ``VisitedText ``, ``ActiveText ``, ``ButtonFace ``, ``ButtonText ``
229
269
230
270
keywords
231
271
........
232
272
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).
234
274
235
275
Examples: ``transparent ``, ``currentColor ``
236
276
237
277
rgb
238
278
...
239
279
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.
241
282
242
283
Examples: ``rgb(255, 255, 255) ``, ``rgb(255,255,255) ``
243
284
244
285
rgba
245
286
....
246
287
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.
248
290
249
291
Examples: ``rgba(255, 255, 255, 0.3) ``, ``rgba(255,255,255,0.3) ``
250
292
251
293
hsl
252
294
...
253
295
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.
255
298
256
299
Examples: ``hsl(0, 0%, 20%) ``, ``hsl(0,0%,20%) ``
257
300
258
301
hsla
259
302
....
260
303
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.
262
306
263
307
Examples: ``hsla(0, 0%, 20%, 0.4) ``, ``hsla(0,0%,20%,0.4) ``
264
308
0 commit comments