|
| 1 | +CssColor |
| 2 | +========= |
| 3 | + |
| 4 | +Validates that a value is a valid CSS color. The underlying value is |
| 5 | +cast to a string before being validated. |
| 6 | + |
| 7 | +========== =================================================================== |
| 8 | +Applies to :ref:`property or method <validation-property-target>` |
| 9 | +Options - `groups`_ |
| 10 | + - `message`_ |
| 11 | + - `mode`_ |
| 12 | + - `normalizer`_ |
| 13 | + - `payload`_ |
| 14 | +Class :class:`Symfony\\Component\\Validator\\Constraints\\CssColor` |
| 15 | +Validator :class:`Symfony\\Component\\Validator\\Constraints\\CssColorValidator` |
| 16 | +========== =================================================================== |
| 17 | + |
| 18 | +Basic Usage |
| 19 | +----------- |
| 20 | + |
| 21 | +.. configuration-block:: |
| 22 | + |
| 23 | + .. code-block:: php-annotations |
| 24 | +
|
| 25 | + // src/Entity/Bulb.php |
| 26 | + namespace App\Entity; |
| 27 | +
|
| 28 | + use Symfony\Component\Validator\Constraints as Assert; |
| 29 | +
|
| 30 | + class Bulb |
| 31 | + { |
| 32 | + /** |
| 33 | + * @Assert\CssColor( |
| 34 | + * message = "The color '{{ value }}' is not a valid hexadecimal color." |
| 35 | + * ) |
| 36 | + */ |
| 37 | + protected $currentColor; |
| 38 | + } |
| 39 | +
|
| 40 | + .. code-block:: yaml |
| 41 | +
|
| 42 | + # config/validator/validation.yaml |
| 43 | + App\Entity\Bulb: |
| 44 | + properties: |
| 45 | + currentColor: |
| 46 | + - CssColor: |
| 47 | + message: The color "{{ value }}" is not a valid hexadecimal color. |
| 48 | +
|
| 49 | + .. code-block:: xml |
| 50 | +
|
| 51 | + <!-- config/validator/validation.xml --> |
| 52 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 53 | + <constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" |
| 54 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 55 | + xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> |
| 56 | +
|
| 57 | + <class name="App\Entity\Bulb"> |
| 58 | + <property name="currentColor"> |
| 59 | + <constraint name="CssColorZ"> |
| 60 | + <option name="message">The color "{{ value }}" is not a valid hexadecimal color.</option> |
| 61 | + </constraint> |
| 62 | + </property> |
| 63 | + </class> |
| 64 | + </constraint-mapping> |
| 65 | +
|
| 66 | + .. code-block:: php |
| 67 | +
|
| 68 | + // src/Entity/Bulb.php |
| 69 | + namespace App\Entity; |
| 70 | +
|
| 71 | + use Symfony\Component\Validator\Constraints as Assert; |
| 72 | + use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 73 | +
|
| 74 | + class Bulb |
| 75 | + { |
| 76 | + public static function loadValidatorMetadata(ClassMetadata $metadata) |
| 77 | + { |
| 78 | + $metadata->addPropertyConstraint('currentColor', new Assert\CssColor([ |
| 79 | + 'message' => 'The color "{{ value }}" is not a valid hexadecimal color.', |
| 80 | + ])); |
| 81 | + } |
| 82 | + } |
| 83 | +
|
| 84 | +.. include:: /reference/constraints/_empty-values-are-valid.rst.inc |
| 85 | + |
| 86 | +Options |
| 87 | +------- |
| 88 | + |
| 89 | +.. include:: /reference/constraints/_groups-option.rst.inc |
| 90 | + |
| 91 | +message |
| 92 | +~~~~~~~ |
| 93 | + |
| 94 | +**type**: ``string`` **default**: ``This value is not a valid hexadecimal color.`` |
| 95 | + |
| 96 | +This message is shown if the underlying data is not a valid hexadecimal color. |
| 97 | + |
| 98 | +You can use the following parameters in this message: |
| 99 | + |
| 100 | +=============== ============================================================== |
| 101 | +Parameter Description |
| 102 | +=============== ============================================================== |
| 103 | +``{{ value }}`` The current (invalid) value |
| 104 | +=============== ============================================================== |
| 105 | + |
| 106 | +mode |
| 107 | +~~~~ |
| 108 | + |
| 109 | +**type**: ``string`` **default**: ``loose`` |
| 110 | + |
| 111 | +This option is optional and defines the pattern the hexadecimal color is validated against. |
| 112 | +Valid values are: |
| 113 | + |
| 114 | +* ``long`` |
| 115 | +* ``short`` |
| 116 | +* ``named_colors`` |
| 117 | +* ``html5`` |
| 118 | + |
| 119 | +long |
| 120 | +..... |
| 121 | + |
| 122 | +A regular expression. Allows all values which represent a hexadecimal color |
| 123 | +of 8 characters (in addition of the leading ``#``) and contained in ranges: 0 to 9 and A to F (case insensitive). |
| 124 | + |
| 125 | +short |
| 126 | +...... |
| 127 | + |
| 128 | +A simple regular expression. Allows all values which represent a hexadecimal color |
| 129 | +of strictly 3 or 4 characters (in addition of the leading ``#``) and contained in ranges: 0 to 9 and A to F (case insensitive). |
| 130 | + |
| 131 | +named_colors |
| 132 | +..... |
| 133 | + |
| 134 | +Accordingly to the `W3C list of named colors`_, it allows to use color by their names. |
| 135 | + |
| 136 | +html5 |
| 137 | +..... |
| 138 | + |
| 139 | +As well as the HTML5 color input, this mode allows all values of strictly 6 characters (in addition of the leading ``#``) and contained in ranges: 0 to 9 and A to F (case insensitive). |
| 140 | + |
| 141 | +.. include:: /reference/constraints/_normalizer-option.rst.inc |
| 142 | + |
| 143 | +.. include:: /reference/constraints/_payload-option.rst.inc |
| 144 | + |
| 145 | +.. _`W3C list of named colors`: https://www.w3.org/TR/css-color-4/#named-color |
0 commit comments