|
| 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 | +``netmaskMin`` |
| 107 | +~~~~~~~~~~~ |
| 108 | + |
| 109 | +**type**: ``int`` **default**: ``0`` |
| 110 | + |
| 111 | +It's a constraint for the lowest value a valid netmask may have. |
| 112 | + |
| 113 | +``netmaskMax`` |
| 114 | +~~~~~~~~~~~ |
| 115 | + |
| 116 | +**type**: ``string`` **default**: ``32 for IPv4 or 128 for IPv6`` |
| 117 | + |
| 118 | +It's a constraint for the biggest value a valid netmask may have. |
| 119 | + |
| 120 | +``netmaskRangeViolationMessage`` |
| 121 | +~~~~~~~~~~~ |
| 122 | + |
| 123 | +**type**: ``string`` **default**: ``The value of the netmask should be between {{ min }} and {{ max }}.`` |
| 124 | + |
| 125 | +This message is shown if the value of the CIDR's netmask is bigger then the value of the `max_` or lower than |
| 126 | +the value of the `min_`. |
| 127 | + |
| 128 | +You can use the following parameters in this message: |
| 129 | + |
| 130 | +=============== ============================================================== |
| 131 | +Parameter Description |
| 132 | +=============== ============================================================== |
| 133 | +``{{ min }}`` The minimum value a CIDR netmask may have |
| 134 | +``{{ max }}`` The maximum value a CIDR netmask may have |
| 135 | +=============== ============================================================== |
| 136 | + |
| 137 | +.. include:: /reference/constraints/_payload-option.rst.inc |
| 138 | + |
| 139 | +``version`` |
| 140 | +~~~~~~~~~~~ |
| 141 | + |
| 142 | +**type**: ``string`` **default**: ``all`` |
| 143 | + |
| 144 | +This determines exactly *how* the CIDR notation is validated and can take one |
| 145 | +of a variety of different values: |
| 146 | + |
| 147 | +**All ranges** |
| 148 | + |
| 149 | +``4`` |
| 150 | + Validates for CIDR notations that have an IPv4. |
| 151 | +``6`` |
| 152 | + Validates for CIDR notations that have an IPv6. |
| 153 | +``all`` |
| 154 | + Validates all CIDR formats |
| 155 | + |
0 commit comments