Skip to content

Commit 26d9346

Browse files
committed
Merge branch '5.4' into 6.0
* 5.4: [Validator] Add documentation for the CidrValidator
2 parents d861654 + f32ee43 commit 26d9346

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed

reference/constraints/Cidr.rst

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
Cidr
2+
====
3+
4+
.. versionadded:: 5.4
5+
6+
The ``Cidr`` constraint was introduced in Symfony 5.4.
7+
8+
Validates that a value is a valid `CIDR`_ (Classless Inter-Domain Routing) notation.
9+
By default, this will validate the CIDR's IP and netmask both for version 4 and 6,
10+
with the option of allowing only one type of IP version to be valid. It also supports
11+
a minimum and maximum range constraint in which the value of the netmask is valid.
12+
13+
========== ===================================================================
14+
Applies to :ref:`property or method <validation-property-target>`
15+
Class :class:`Symfony\\Component\\Validator\\Constraints\\Cidr`
16+
Validator :class:`Symfony\\Component\\Validator\\Constraints\\CidrValidator`
17+
========== ===================================================================
18+
19+
Basic Usage
20+
-----------
21+
22+
.. configuration-block::
23+
24+
.. code-block:: php-annotations
25+
26+
// src/Entity/Author.php
27+
namespace App\Entity;
28+
29+
use Symfony\Component\Validator\Constraints as Assert;
30+
31+
class Author
32+
{
33+
/**
34+
* @Assert\Cidr
35+
*/
36+
protected $cidrNotation;
37+
}
38+
39+
.. code-block:: php-attributes
40+
41+
// src/Entity/Author.php
42+
namespace App\Entity;
43+
44+
use Symfony\Component\Validator\Constraints as Assert;
45+
46+
class Author
47+
{
48+
#[Assert\Cidr]
49+
protected $cidrNotation;
50+
}
51+
52+
.. code-block:: yaml
53+
54+
# config/validator/validation.yaml
55+
App\Entity\Author:
56+
properties:
57+
cidrNotation:
58+
- Cidr: ~
59+
60+
.. code-block:: xml
61+
62+
<!-- config/validator/validation.xml -->
63+
<?xml version="1.0" encoding="UTF-8" ?>
64+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
65+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
66+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
67+
68+
<class name="App\Entity\Author">
69+
<property name="cidrNotation">
70+
<constraint name="Cidr"/>
71+
</property>
72+
</class>
73+
</constraint-mapping>
74+
75+
.. code-block:: php
76+
77+
// src/Entity/Author.php
78+
namespace App\Entity;
79+
80+
use Symfony\Component\Validator\Constraints as Assert;
81+
use Symfony\Component\Validator\Mapping\ClassMetadata;
82+
83+
class Author
84+
{
85+
public static function loadValidatorMetadata(ClassMetadata $metadata)
86+
{
87+
$metadata->addPropertyConstraint('cidrNotation', new Assert\Cidr());
88+
}
89+
}
90+
91+
.. include:: /reference/constraints/_empty-values-are-valid.rst.inc
92+
93+
Options
94+
-------
95+
96+
.. include:: /reference/constraints/_groups-option.rst.inc
97+
98+
``message``
99+
~~~~~~~~~~~
100+
101+
**type**: ``string`` **default**: ``This value is not a valid CIDR notation.``
102+
103+
This message is shown if the string is not a valid CIDR notation.
104+
105+
``netmaskMin``
106+
~~~~~~~~~~~
107+
108+
**type**: ``integer`` **default**: ``0``
109+
110+
It's a constraint for the lowest value a valid netmask may have.
111+
112+
``netmaskMax``
113+
~~~~~~~~~~~
114+
115+
**type**: ``string`` **default**: ``32 for IPv4 or 128 for IPv6``
116+
117+
It's a constraint for the biggest value a valid netmask may have.
118+
119+
``netmaskRangeViolationMessage``
120+
~~~~~~~~~~~
121+
122+
**type**: ``string`` **default**: ``The value of the netmask should be between {{ min }} and {{ max }}.``
123+
124+
This message is shown if the value of the CIDR's netmask is bigger then the value of the `max_` or lower than
125+
the value of the `min_`.
126+
127+
You can use the following parameters in this message:
128+
129+
=============== ==============================================================
130+
Parameter Description
131+
=============== ==============================================================
132+
``{{ min }}`` The minimum value a CIDR netmask may have
133+
``{{ max }}`` The maximum value a CIDR netmask may have
134+
=============== ==============================================================
135+
136+
.. include:: /reference/constraints/_payload-option.rst.inc
137+
138+
``version``
139+
~~~~~~~~~~~
140+
141+
**type**: ``string`` **default**: ``all``
142+
143+
This determines exactly *how* the CIDR notation is validated and can take one
144+
of a variety of different values:
145+
146+
**All ranges**
147+
148+
``4``
149+
Validates for CIDR notations that have an IPv4.
150+
``6``
151+
Validates for CIDR notations that have an IPv6.
152+
``all``
153+
Validates all CIDR formats
154+
155+
.. _`CIDR`: https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing

reference/constraints/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ String Constraints
2222
* :doc:`Regex </reference/constraints/Regex>`
2323
* :doc:`Hostname </reference/constraints/Hostname>`
2424
* :doc:`Ip </reference/constraints/Ip>`
25+
* :doc:`Cidr </reference/constraints/Cidr>`
2526
* :doc:`Json </reference/constraints/Json>`
2627
* :doc:`Uuid </reference/constraints/Uuid>`
2728
* :doc:`Ulid </reference/constraints/Ulid>`

0 commit comments

Comments
 (0)