Skip to content

Latest commit

 

History

History
150 lines (104 loc) · 4.52 KB

Cidr.rst

File metadata and controls

150 lines (104 loc) · 4.52 KB

Cidr

.. versionadded:: 5.4

    The ``Cidr`` constraint was introduced in Symfony 5.4.

Validates that a value is a valid CIDR (Classless Inter-Domain Routing) notation. By default, this will validate the CIDR's IP and netmask both for version 4 and 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 <validation-property-target>`
Class :class:`Symfony\\Component\\Validator\\Constraints\\Cidr`
Validator :class:`Symfony\\Component\\Validator\\Constraints\\CidrValidator`

Basic Usage

.. configuration-block::

    .. code-block:: php-annotations

        // src/Entity/NetworkSettings.php
        namespace App\Entity;

        use Symfony\Component\Validator\Constraints as Assert;

        class NetworkSettings
        {
            /**
             * @Assert\Cidr
             */
            protected $cidrNotation;
        }

    .. code-block:: php-attributes

        // src/Entity/NetworkSettings.php
        namespace App\Entity;

        use Symfony\Component\Validator\Constraints as Assert;

        class NetworkSettings
        {
            #[Assert\Cidr]
            protected $cidrNotation;
        }

    .. code-block:: yaml

        # config/validator/validation.yaml
        App\Entity\NetworkSettings:
            properties:
                cidrNotation:
                    - Cidr: ~

    .. code-block:: xml

        <!-- config/validator/validation.xml -->
        <?xml version="1.0" encoding="UTF-8" ?>
        <constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

            <class name="App\Entity\NetworkSettings">
                <property name="cidrNotation">
                    <constraint name="Cidr"/>
                </property>
            </class>
        </constraint-mapping>

    .. code-block:: php

        // src/Entity/NetworkSettings.php
        namespace App\Entity;

        use Symfony\Component\Validator\Constraints as Assert;
        use Symfony\Component\Validator\Mapping\ClassMetadata;

        class NetworkSettings
        {
            public static function loadValidatorMetadata(ClassMetadata $metadata)
            {
                $metadata->addPropertyConstraint('cidrNotation', new Assert\Cidr());
            }
        }

Options

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: integer 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 than the netmaskMax value or lower than the netmaskMin value.

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

version

type: string default: all

This determines exactly how the CIDR notation is validated and can take one of these values:

  • 4: validates for CIDR notations that have an IPv4;
  • 6: validates for CIDR notations that have an IPv6;
  • all: validates all CIDR formats.