diff --git a/reference/constraints/Cidr.rst b/reference/constraints/Cidr.rst new file mode 100644 index 00000000000..8f415162437 --- /dev/null +++ b/reference/constraints/Cidr.rst @@ -0,0 +1,155 @@ +Cidr +== + +Validates that a value is a valid CIDR notation. By default, this will validate +the CIDR's IP and netmask both for version 4 and version 6, with the option of allowing +only one type of IP version to be valid. It also supports a minimum and maximum range +constraint in which the value of the netmask is valid. + +========== =================================================================== +Applies to :ref:`property or method ` +Options - `groups`_ + - `message`_ + - `netmaskRangeViolationMessage`_ + - `payload`_ + - `version`_ +Class :class:`Symfony\\Component\\Validator\\Constraints\\Cidr` +Validator :class:`Symfony\\Component\\Validator\\Constraints\\CidrValidator` +========== =================================================================== + +Basic Usage +----------- + +.. configuration-block:: + + .. code-block:: php-annotations + + // src/Entity/Author.php + namespace App\Entity; + + use Symfony\Component\Validator\Constraints as Assert; + + class Author + { + /** + * @Assert\Cidr + */ + protected $cidrNotation; + } + + .. code-block:: php-attributes + + // src/Entity/Author.php + namespace App\Entity; + + use Symfony\Component\Validator\Constraints as Assert; + + class Author + { + #[Assert\Cidr] + protected $cidrNotation; + } + + .. code-block:: yaml + + # config/validator/validation.yaml + App\Entity\Author: + properties: + cidrNotation: + - Cidr: ~ + + .. code-block:: xml + + + + + + + + + + + + + .. code-block:: php + + // src/Entity/Author.php + namespace App\Entity; + + use Symfony\Component\Validator\Constraints as Assert; + use Symfony\Component\Validator\Mapping\ClassMetadata; + + class Author + { + public static function loadValidatorMetadata(ClassMetadata $metadata) + { + $metadata->addPropertyConstraint('cidrNotation', new Assert\Cidr()); + } + } + +.. include:: /reference/constraints/_empty-values-are-valid.rst.inc + +Options +------- + +.. include:: /reference/constraints/_groups-option.rst.inc + +``message`` +~~~~~~~~~~~ + +**type**: ``string`` **default**: ``This value is not a valid CIDR notation.`` + +This message is shown if the string is not a valid CIDR notation. + +``netmaskMin`` +~~~~~~~~~~~ + +**type**: ``integer`` **default**: ``0`` + +It's a constraint for the lowest value a valid netmask may have. + +``netmaskMax`` +~~~~~~~~~~~ + +**type**: ``string`` **default**: ``32 for IPv4 or 128 for IPv6`` + +It's a constraint for the biggest value a valid netmask may have. + +``netmaskRangeViolationMessage`` +~~~~~~~~~~~ + +**type**: ``string`` **default**: ``The value of the netmask should be between {{ min }} and {{ max }}.`` + +This message is shown if the value of the CIDR's netmask is bigger then the value of the `max_` or lower than +the value of the `min_`. + +You can use the following parameters in this message: + +=============== ============================================================== +Parameter Description +=============== ============================================================== +``{{ min }}`` The minimum value a CIDR netmask may have +``{{ max }}`` The maximum value a CIDR netmask may have +=============== ============================================================== + +.. include:: /reference/constraints/_payload-option.rst.inc + +``version`` +~~~~~~~~~~~ + +**type**: ``string`` **default**: ``all`` + +This determines exactly *how* the CIDR notation is validated and can take one +of a variety of different values: + +**All ranges** + +``4`` + Validates for CIDR notations that have an IPv4. +``6`` + Validates for CIDR notations that have an IPv6. +``all`` + Validates all CIDR formats +