8000 minor #18568 [Validator] Add `EnableAutoMapping` and `DisableAutoMapp… · symfony/symfony-docs@19955a7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 19955a7

Browse files
committed
minor #18568 [Validator] Add EnableAutoMapping and DisableAutoMapping constraints (alexandre-daubois)
This PR was merged into the 5.4 branch. Discussion ---------- [Validator] Add `EnableAutoMapping` and `DisableAutoMapping` constraints Fix #12563 Commits ------- 2653b56 [Validator] Add `EnableAutoMapping` and `DisableAutoMapping` constraints
2 parents 97e2682 + 2653b56 commit 19955a7

File tree

5 files changed

+244
-0
lines changed

5 files changed

+244
-0
lines changed

reference/configuration/doctrine.rst

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

302+
.. _doctrine_auto-mapping:
303+
302304
Custom Mapping Entities in a Bundle
303305
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
304306

reference/constraints.rst

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

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

0 commit comments

Comments
 (0)
0