Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf395714

Browse files
committed
Added CssColor constraint
1 parent5f75c74 commitf395714

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-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: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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+
- `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-mappingxmlns="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+
<classname="App\Entity\Bulb">
58+
<propertyname="currentColor">
59+
<constraintname="CssColor">
60+
<optionname="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

‎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)

[8]ページ先頭

©2009-2025 Movatter.jp