|
| 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 | + * formats = { Assert\CssColor::HEX_LONG } |
| 34 | + * message = "The color '{{ value }}' is not a valid CSS color." |
| 35 | + * ) |
| 36 | + */ |
| 37 | + protected $defaultColor; |
| 38 | +
|
| 39 | + /** |
| 40 | + * @Assert\CssColor( |
| 41 | + * formats = Assert\CssColor::BASIC_NAMED_COLORS |
| 42 | + * message = "The color '{{ value }}' is not a valid CSS color." |
| 43 | + * ) |
| 44 | + */ |
| 45 | + protected $currentColor; |
| 46 | + } |
| 47 | +
|
| 48 | + .. code-block:: php-attributes |
| 49 | +
|
| 50 | + // src/Entity/Bulb.php |
| 51 | + namespace App\Entity; |
| 52 | +
|
| 53 | + use Symfony\Component\Validator\Constraints as Assert; |
| 54 | +
|
| 55 | + class Bulb |
| 56 | + { |
| 57 | + #[Assert\CssColor( |
| 58 | + formats: [Assert\CssColor::HEX_LONG] |
| 59 | + message: 'The color '{{ value }}' is not a valid CSS color.', |
| 60 | + )] |
| 61 | + protected $defaultColor; |
| 62 | +
|
| 63 | + #[Assert\CssColor( |
| 64 | + formats: Assert\CssColor::BASIC_NAMED_COLORS |
| 65 | + message: 'The color '{{ value }}' is not a valid CSS color.', |
| 66 | + )] |
| 67 | + protected $currentColor; |
| 68 | + } |
| 69 | +
|
| 70 | + .. code-block:: yaml |
| 71 | +
|
| 72 | + # config/validator/validation.yaml |
| 73 | + App\Entity\Bulb: |
| 74 | + properties: |
| 75 | + defaultColor: |
| 76 | + - CssColor: |
| 77 | + formats: [ !php/const Symfony\Component\Validator\Constraints\CssColor::HEX_LONG ] |
| 78 | + message: The color "{{ value }}" is not a valid CSS color. |
| 79 | + currentColor: |
| 80 | + - CssColor: |
| 81 | + formats: !php/const Symfony\Component\Validator\Constraints\CssColor::BASIC_NAMED_COLORS |
| 82 | + message: The color "{{ value }}" is not a valid CSS color. |
| 83 | +
|
| 84 | + .. code-block:: xml |
| 85 | +
|
| 86 | + <!-- config/validator/validation.xml --> |
| 87 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 88 | + <constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" |
| 89 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 90 | + xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> |
| 91 | +
|
| 92 | + <class name="App\Entity\Bulb"> |
| 93 | + <property name="currentColor"> |
| 94 | + <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> |
| 99 | + </constraint> |
| 100 | + </property> |
| 101 | + <property name="defaultColor"> |
| 102 | + <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> |
| 105 | + </constraint> |
| 106 | + </property> |
| 107 | + </class> |
| 108 | + </constraint-mapping> |
| 109 | +
|
| 110 | + .. code-block:: php |
| 111 | +
|
| 112 | + // src/Entity/Bulb.php |
| 113 | + namespace App\Entity; |
| 114 | +
|
| 115 | + use Symfony\Component\Validator\Constraints as Assert; |
| 116 | + use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 117 | +
|
| 118 | + class Bulb |
| 119 | + { |
| 120 | + public static function loadValidatorMetadata(ClassMetadata $metadata) |
| 121 | + { |
| 122 | + $metadata->addPropertyConstraint('defaultColor', new Assert\CssColor([ |
| 123 | + 'formats' => [Assert\CssColor::HEX_LONG], |
| 124 | + 'message' => 'The color "{{ value }}" is not a valid CSS color.', |
| 125 | + ])); |
| 126 | +
|
| 127 | + $metadata->addPropertyConstraint('currentColor', new Assert\CssColor([ |
| 128 | + 'formats' => Assert\CssColor::BASIC_NAMED_COLORS, |
| 129 | + 'message' => 'The color "{{ value }}" is not a valid CSS color.', |
| 130 | + ])); |
| 131 | + } |
| 132 | + } |
| 133 | +
|
| 134 | +.. include:: /reference/constraints/_empty-values-are-valid.rst.inc |
| 135 | + |
| 136 | +Options |
| 137 | +------- |
| 138 | + |
| 139 | +.. include:: /reference/constraints/_groups-option.rst.inc |
| 140 | + |
| 141 | +message |
| 142 | +~~~~~~~ |
| 143 | + |
| 144 | +**type**: ``string`` **default**: ``This value is not a valid CSS color.`` |
| 145 | + |
| 146 | +This message is shown if the underlying data is not a valid CSS color. |
| 147 | + |
| 148 | +You can use the following parameters in this message: |
| 149 | + |
| 150 | +=============== ============================================================== |
| 151 | +Parameter Description |
| 152 | +=============== ============================================================== |
| 153 | +``{{ value }}`` The current (invalid) value |
| 154 | +=============== ============================================================== |
| 155 | + |
| 156 | +formats |
| 157 | +~~~~~~~ |
| 158 | + |
| 159 | +**type**: ``string`` | ``array`` |
| 160 | + |
| 161 | +This option is optional and defines the pattern the CSS color is validated against. |
| 162 | +Valid values are: |
| 163 | + |
| 164 | +* ``hex_long`` |
| 165 | +* ``hex_long_with_alpha`` |
| 166 | +* ``hex_short`` |
| 167 | +* ``hex_short_with_alpha`` |
| 168 | +* ``basic_named_colors`` |
| 169 | +* ``extended_named_colors`` |
| 170 | +* ``system_colors`` |
| 171 | +* ``keywords`` |
| 172 | +* ``rgb`` |
| 173 | +* ``rgba`` |
| 174 | +* ``hsl`` |
| 175 | +* ``hsla`` |
| 176 | + |
| 177 | +hex_long |
| 178 | +........ |
| 179 | + |
| 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). |
| 182 | + |
| 183 | +Examples: ``#2F2F2F``, ``#2f2f2f`` |
| 184 | + |
| 185 | +hex_long_with_alpha |
| 186 | +................... |
| 187 | + |
| 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). |
| 190 | + |
| 191 | +Examples: ``#2F2F2F80``, ``#2f2f2f80`` |
| 192 | + |
| 193 | +hex_short |
| 194 | +......... |
| 195 | + |
| 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). |
| 198 | + |
| 199 | +Examples: ``#CCC``, ``#ccc`` |
| 200 | + |
| 201 | +hex_short_with_alpha |
| 202 | +.................... |
| 203 | + |
| 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). |
| 206 | + |
| 207 | +Examples: ``#CCC8``, ``#ccc8`` |
| 208 | + |
| 209 | +basic_named_colors |
| 210 | +.................. |
| 211 | + |
| 212 | +Accordingly to the `W3C list of basic named colors`_, it allows to use colors by their names (case insensitive). |
| 213 | + |
| 214 | +Examples: ``black``, ``red``, ``green`` |
| 215 | + |
| 216 | +extended_named_colors |
| 217 | +..................... |
| 218 | + |
| 219 | +Accordingly to the `W3C list of extended named colors`_, it allows to use colors by their names (case insensitive). |
| 220 | + |
| 221 | +Examples: ``aqua``, ``brown``, ``chocolate`` |
| 222 | + |
| 223 | +system_colors |
| 224 | +............. |
| 225 | + |
| 226 | +Accordingly to the `CSS WG list of system colors`_, it allows to use colors by their names (case insensitive). |
| 227 | + |
| 228 | +Examples: ``LinkText``, ``VisitedText``, ``ActiveText``, ``ButtonFace``, ``ButtonText`` |
| 229 | + |
| 230 | +keywords |
| 231 | +........ |
| 232 | + |
| 233 | +Accordingly to the `CSS WG list of keywords`_, it allows to use colors by their names (case insensitive). |
| 234 | + |
| 235 | +Examples: ``transparent``, ``currentColor`` |
| 236 | + |
| 237 | +rgb |
| 238 | +... |
| 239 | + |
| 240 | +A regular expression. Allows all values which represent a CSS color following th RGB notation, with or without space between values. |
| 241 | + |
| 242 | +Examples: ``rgb(255, 255, 255)``, ``rgb(255,255,255)`` |
| 243 | + |
| 244 | +rgba |
| 245 | +.... |
| 246 | + |
| 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. |
| 248 | + |
| 249 | +Examples: ``rgba(255, 255, 255, 0.3)``, ``rgba(255,255,255,0.3)`` |
| 250 | + |
| 251 | +hsl |
| 252 | +... |
| 253 | + |
| 254 | +A regular expression. Allows all values which represent a CSS color following th HSL notation, with or without space between values. |
| 255 | + |
| 256 | +Examples: ``hsl(0, 0%, 20%)``, ``hsl(0,0%,20%)`` |
| 257 | + |
| 258 | +hsla |
| 259 | +.... |
| 260 | + |
| 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. |
| 262 | + |
| 263 | +Examples: ``hsla(0, 0%, 20%, 0.4)``, ``hsla(0,0%,20%,0.4)`` |
| 264 | + |
| 265 | +.. include:: /reference/constraints/_payload-option.rst.inc |
| 266 | + |
| 267 | +.. _`W3C list of basic named colors`: https://www.w3.org/wiki/CSS/Properties/color/keywords#Basic_Colors |
| 268 | +.. _`W3C list of extended named colors`: https://www.w3.org/wiki/CSS/Properties/color/keywords#Extended_colors |
| 269 | +.. _`CSS WG list of system colors`: https://drafts.csswg.org/css-color/#css-system-colors |
| 270 | +.. _`CSS WG list of keywords`: https://drafts.csswg.org/css-color/#transparent-color |
0 commit comments