Skip to content

Commit 5771f7b

Browse files
committed
[Validator] Add documentation for the CidrValidator
1 parent 69cfed5 commit 5771f7b

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

reference/constraints/Cidr.rst

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

0 commit comments

Comments
 (0)