From 597a0886e03b9a68560aa82e76465e1c6acfc444 Mon Sep 17 00:00:00 2001 From: Antoine Lamirault Date: Sat, 6 Jan 2024 12:12:41 +0100 Subject: [PATCH] [Validator] Add constraint --- reference/constraints/MacAddress.rst | 104 +++++++++++++++++++++++++++ reference/constraints/map.rst.inc | 1 + 2 files changed, 105 insertions(+) create mode 100644 reference/constraints/MacAddress.rst diff --git a/reference/constraints/MacAddress.rst b/reference/constraints/MacAddress.rst new file mode 100644 index 00000000000..e13420bd832 --- /dev/null +++ b/reference/constraints/MacAddress.rst @@ -0,0 +1,104 @@ +MacAddress +========== + +.. versionadded:: 7.1 + + The ``MacAddress`` constraint was introduced in Symfony 7.1. + +This constraint ensures that the given value is a valid MAC address (internally it +uses the ``FILTER_VALIDATE_MAC`` option of the :phpfunction:`filter_var` PHP +function). + +========== ===================================================================== +Applies to :ref:`property or method ` +Class :class:`Symfony\\Component\\Validator\\Constraints\\MacAddress` +Validator :class:`Symfony\\Component\\Validator\\Constraints\\MacAddressValidator` +========== ===================================================================== + +Basic Usage +----------- + +To use the MacAddress validator, apply it to a property on an object that +will contain a host name. + +.. configuration-block:: + + .. code-block:: php-attributes + + // src/Entity/Author.php + namespace App\Entity; + + use Symfony\Component\Validator\Constraints as Assert; + + class Author + { + #[Assert\MacAddress] + protected string $mac; + } + + .. code-block:: yaml + + # config/validator/validation.yaml + App\Entity\Author: + properties: + mac: + - MacAddress: ~ + + .. 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): void + { + $metadata->addPropertyConstraint('mac', new Assert\MacAddress()); + } + } + +.. include:: /reference/constraints/_empty-values-are-valid.rst.inc + +Options +------- + +.. include:: /reference/constraints/_groups-option.rst.inc + +``message`` +~~~~~~~~~~~ + +**type**: ``string`` **default**: ``This is not a valid MAC address.`` + +This is the message that will be shown if the value is not a valid MAC address. + +You can use the following parameters in this message: + +=============== ============================================================== +Parameter Description +=============== ============================================================== +``{{ value }}`` The current (invalid) value +=============== ============================================================== + +.. include:: /reference/constraints/_normalizer-option.rst.inc + +.. include:: /reference/constraints/_payload-option.rst.inc diff --git a/reference/constraints/map.rst.inc b/reference/constraints/map.rst.inc index a9692062d28..690e98c6bf9 100644 --- a/reference/constraints/map.rst.inc +++ b/reference/constraints/map.rst.inc @@ -32,6 +32,7 @@ String Constraints * :doc:`CssColor ` * :doc:`NoSuspiciousCharacters ` * :doc:`Charset ` +* :doc:`MacAddress ` Comparison Constraints ~~~~~~~~~~~~~~~~~~~~~~