Skip to content

Commit 597a088

Browse files
committed
[Validator] Add constraint
1 parent bb03a64 commit 597a088

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

reference/constraints/MacAddress.rst

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
MacAddress
2+
==========
3+
4+
.. versionadded:: 7.1
5+
6+
The ``MacAddress`` constraint was introduced in Symfony 7.1.
7+
8+
This constraint ensures that the given value is a valid MAC address (internally it
9+
uses the ``FILTER_VALIDATE_MAC`` option of the :phpfunction:`filter_var` PHP
10+
function).
11+
12+
========== =====================================================================
13+
Applies to :ref:`property or method <validation-property-target>`
14+
Class :class:`Symfony\\Component\\Validator\\Constraints\\MacAddress`
15+
Validator :class:`Symfony\\Component\\Validator\\Constraints\\MacAddressValidator`
16+
========== =====================================================================
17+
18+
Basic Usage
19+
-----------
20+
21+
To use the MacAddress validator, apply it to a property on an object that
22+
will contain a host name.
23+
24+
.. configuration-block::
25+
26+
.. code-block:: php-attributes
27+
28+
// src/Entity/Author.php
29+
namespace App\Entity;
30+
31+
use Symfony\Component\Validator\Constraints as Assert;
32+
33+
class Author
34+
{
35+
#[Assert\MacAddress]
36+
protected string $mac;
37+
}
38+
39+
.. code-block:: yaml
40+
41+
# config/validator/validation.yaml
42+
App\Entity\Author:
43+
properties:
44+
mac:
45+
- MacAddress: ~
46+
47+
.. code-block:: xml
48+
49+
<!-- config/validator/validation.xml -->
50+
<?xml version="1.0" encoding="UTF-8" ?>
51+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
52+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
53+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
54+
55+
<class name="App\Entity\Author">
56+
<property name="max">
57+
<constraint name="MacAddress"/>
58+
</property>
59+
</class>
60+
</constraint-mapping>
61+
62+
.. code-block:: php
63+
64+
// src/Entity/Author.php
65+
namespace App\Entity;
66+
67+
use Symfony\Component\Validator\Constraints as Assert;
68+
use Symfony\Component\Validator\Mapping\ClassMetadata;
69+
70+
class Author
71+
{
72+
// ...
73+
74+
public static function loadValidatorMetadata(ClassMetadata $metadata): void
75+
{
76+
$metadata->addPropertyConstraint('mac', new Assert\MacAddress());
77+
}
78+
}
79+
80+
.. include:: /reference/constraints/_empty-values-are-valid.rst.inc
81+
82+
Options
83+
-------
84+
85+
.. include:: /reference/constraints/_groups-option.rst.inc
86+
87+
``message``
88+
~~~~~~~~~~~
89+
90+
**type**: ``string`` **default**: ``This is not a valid MAC address.``
91+
92+
This is the message that will be shown if the value is not a valid MAC address.
93+
94+
You can use the following parameters in this message:
95+
96+
=============== ==============================================================
97+
Parameter Description
98+
=============== ==============================================================
99+
``{{ value }}`` The current (invalid) value
100+
=============== ==============================================================
101+
102+
.. include:: /reference/constraints/_normalizer-option.rst.inc
103+
104+
.. include:: /reference/constraints/_payload-option.rst.inc

reference/constraints/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ String Constraints
3232
* :doc:`CssColor </reference/constraints/CssColor>`
3333
* :doc:`NoSuspiciousCharacters </reference/constraints/NoSuspiciousCharacters>`
3434
* :doc:`Charset </reference/constraints/Charset>`
35+
* :doc:`MacAddress </reference/constraints/MacAddress>`
3536

3637
Comparison Constraints
3738
~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)