Skip to content

Commit 5f4b8d0

Browse files
committed
Merge branch '5.4' into 6.2
* 5.4: Tweaks [Validator] Add `EnableAutoMapping` and `DisableAutoMapping` constraints
2 parents 57fe42b + ffb2cc1 commit 5f4b8d0

File tree

5 files changed

+242
-0
lines changed

5 files changed

+242
-0
lines changed

reference/configuration/doctrine.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ This option is ``false`` by default and it's considered a legacy option. It was
301301
only useful in previous Symfony versions, when it was recommended to use bundles
302302
to organize the application code.
303303

304+
.. _doctrine_auto-mapping:
305+
304306
Custom Mapping Entities in a Bundle
305307
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
306308

reference/constraints.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ Validation Constraints Reference
7979
constraints/Traverse
8080
constraints/CssColor
8181
constraints/Cascade
82+
constraints/EnableAutoMapping
83+
constraints/DisableAutoMapping
8284

8385
The Validator is designed to validate objects against *constraints*.
8486
In real life, a constraint could be: "The cake must not be burned". In
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
DisableAutoMapping
2+
==================
3+
4+
This constraint allows to disable :ref:`Doctrine's auto mapping <doctrine_auto-mapping>`
5+
on a class or a property. Automapping allows to determine validation rules based
6+
on Doctrine's annotations and attributes. You may use this constraint when
7+
automapping is globally enabled, but you still want to disable this feature for
8+
a class or a property specifically.
9+
10+
========== ===================================================================
11+
Applies to :ref:`property or method <validation-property-target>`
12+
Class :class:`Symfony\\Component\\Validator\\Constraints\\DisableAutoMapping`
13+
========== ===================================================================
14+
15+
Basic Usage
16+
-----------
17+
18+
In the following example, the
19+
:class:`Symfony\\Component\\Validator\\Constraints\\DisableAutoMapping`
20+
constraint will tell the validator to not gather constraints from Doctrine's
21+
metadata:
22+
23+
.. configuration-block::
24+
25+
.. code-block:: php-annotations
26+
27+
// src/Model/BookCollection.php
28+
namespace App\Model;
29+
30+
use App\Model\Author;
31+
use App\Model\BookMetadata;
32+
use Doctrine\ORM\Mapping as ORM;
33+
use Symfony\Component\Validator\Constraints as Assert;
34+
35+
/**
36+
* @Assert\DisableAutoMapping
37+
*/
38+
class BookCollection
39+
{
40+
/**
41+
* @ORM\Column(nullable=false)
42+
*/
43+
protected $name = '';
44+
45+
/**
46+
* @ORM\ManyToOne(targetEntity=Author::class)
47+
*/
48+
public Author $author;
49+
50+
// ...
51+
}
52+
53+
.. code-block:: php-attributes
54+
55+
// src/Model/BookCollection.php
56+
namespace App\Model;
57+
58+
use App\Model\Author;
59+
use App\Model\BookMetadata;
60+
use Doctrine\ORM\Mapping as ORM;
61+
use Symfony\Component\Validator\Constraints as Assert;
62+
63+
#[Assert\DisableAutoMapping]
64+
class BookCollection
65+
{
66+
#[ORM\Column(nullable: false)]
67+
protected string $name = '';
68+
69+
#[ORM\ManyToOne(targetEntity: Author::class)]
70+
public Author $author;
71+
72+
// ...
73+
}
74+
75+
.. code-block:: yaml
76+
77+
# config/validator/validation.yaml
78+
App\Entity\BookCollection:
79+
constraints:
80+
- DisableAutoMapping: ~
81+
82+
.. code-block:: xml
83+
84+
<!-- config/validator/validation.xml -->
85+
<?xml version="1.0" encoding="UTF-8" ?>
86+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
87+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
88+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
89+
90+
<class name="App\Entity\BookCollection">
91+
<constraint name="DisableAutoMapping"/>
92+
</class>
93+
</constraint-mapping>
94+
95+
.. code-block:: php
96+
97+
// src/Entity/BookCollection.php
98+
namespace App\Entity;
99+
100+
use Symfony\Component\Validator\Constraints as Assert;
101+
use Symfony\Component\Validator\Mapping\ClassMetadata;
102+
103+
class BookCollection
104+
{
105+
// ...
106+
107+
public static function loadValidatorMetadata(ClassMetadata $metadata)
108+
{
109+
$metadata->addConstraint(new Assert\DisableAutoMapping());
110+
}
111+
}
112+
113+
Options
114+
-------
115+
116+
The ``groups`` option is not available for this constraint.
117+
118+
.. include:: /reference/constraints/_payload-option.rst.inc
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
EnableAutoMapping
2+
=================
3+
4+
This constraint allows to enable :ref:`Doctrine's auto mapping <doctrine_auto-mapping>`
5+
on a class or a property. Automapping allows to determine validation rules based
6+
on Doctrine's annotations and attributes. You may use this constraint when
7+
automapping is globally disabled, but you still want to enable this feature for
8+
a class or a property specifically.
9+
10+
========== ===================================================================
11+
Applies to :ref:`property or method <validation-property-target>`
12+
Class :class:`Symfony\\Component\\Validator\\Constraints\\EnableAutoMapping`
13+
========== ===================================================================
14+
15+
Basic Usage
16+
-----------
17+
18+
In the following example, the
19+
:class:`Symfony\\Component\\Validator\\Constraints\\EnableAutoMapping`
20+
constraint will tell the validator to gather constraints from Doctrine's
21+
metadata:
22+
23+
.. configuration-block::
24+
25+
.. code-block:: php-annotations
26+
27+
// src/Model/BookCollection.php
28+
namespace App\Model;
29+
30+
use App\Model\Author;
31+
use App\Model\BookMetadata;
32+
use Doctrine\ORM\Mapping as ORM;
33+
use Symfony\Component\Validator\Constraints as Assert;
34+
35+
/**
36+
* @Assert\EnableAutoMapping
37+
*/
38+
class BookCollection
39+
{
40+
/**
41+
* @ORM\Column(nullable=false)
42+
*/
43+
protected $name = '';
44+
45+
/**
46+
* @ORM\ManyToOne(targetEntity=Author::class)
47+
*/
48+
public Author $author;
49+
50+
// ...
51+
}
52+
53+
.. code-block:: php-attributes
54+
55+
// src/Model/BookCollection.php
56+
namespace App\Model;
57+
58+
use App\Model\Author;
59+
use App\Model\BookMetadata;
60+
use Doctrine\ORM\Mapping as ORM;
61+
use Symfony\Component\Validator\Constraints as Assert;
62+
63+
#[Assert\EnableAutoMapping]
64+
class BookCollection
65+
{
66+
#[ORM\Column(nullable: false)]
67+
protected string $name = '';
68+
69+
#[ORM\ManyToOne(targetEntity: Author::class)]
70+
public Author $author;
71+
72+
// ...
73+
}
74+
75+
.. code-block:: yaml
76+
77+
# config/validator/validation.yaml
78+
App\Entity\BookCollection:
79+
constraints:
80+
- EnableAutoMapping: ~
81+
82+
.. code-block:: xml
83+
84+
<!-- config/validator/validation.xml -->
85+
<?xml version="1.0" encoding="UTF-8" ?>
86+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
87+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
88+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
89+
90+
<class name="App\Entity\BookCollection">
91+
<constraint name="EnableAutoMapping"/>
92+
</class>
93+
</constraint-mapping>
94+
95+
.. code-block:: php
96+
97+
// src/Entity/BookCollection.php
98+
namespace App\Entity;
99+
100+
use Symfony\Component\Validator\Constraints as Assert;
101+
use Symfony\Component\Validator\Mapping\ClassMetadata;
102+
103+
class BookCollection
104+
{
105+
// ...
106+
107+
public static function loadValidatorMetadata(ClassMetadata $metadata)
108+
{
109+
$metadata->addConstraint(new Assert\EnableAutoMapping());
110+
}
111+
}
112+
113+
Options
114+
-------
115+
116+
The ``groups`` option is not available for this constraint.
117+
118+
.. include:: /reference/constraints/_payload-option.rst.inc

reference/constraints/map.rst.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,5 @@ Other Constraints
102102
* :doc:`Collection </reference/constraints/Collection>`
103103
* :doc:`Count </reference/constraints/Count>`
104104
* :doc:`UniqueEntity </reference/constraints/UniqueEntity>`
105+
* :doc:`EnableAutoMapping </reference/constraints/EnableAutoMapping>`
106+
* :doc:`DisableAutoMapping </reference/constraints/DisableAutoMapping>`

0 commit comments

Comments
 (0)