8000 Merge branch '6.3' into 6.4 · symfony/symfony-docs@9b2f49f · GitHub
[go: up one dir, main page]

Skip to content

Commit 9b2f49f

Browse files
committed
Merge branch '6.3' into 6.4
* 6.3: Remove annotations example [Validator] Add `EnableAutoMapping` and `DisableAutoMapping` constraints
2 parents fcd1670 + 3ecd73c commit 9b2f49f

File tree

5 files changed

+186
-0
lines changed

5 files changed

+186
-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
@@ -80,6 +80,8 @@ Validation Constraints Reference
8080
constraints/Traverse
8181
constraints/CssColor
8282
constraints/Cascade
83+
constraints/EnableAutoMapping
84+
constraints/DisableAutoMapping
8385
constraints/NoSuspiciousCharacters
8486

8587
The Validator is designed to validate objects against *constraints*.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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-attributes
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+
#[Assert\DisableAutoMapping]
36+
class BookCollection
37+
{
38+
#[ORM\Column(nullable: false)]
39+
protected string $name = '';
40+
41+
#[ORM\ManyToOne(targetEntity: Author::class)]
42+
public Author $author;
43+
44+
// ...
45+
}
46+
47+
.. code-block:: yaml
48+
49+
# config/validator/validation.yaml
50+
App\Entity\BookCollection:
51+
constraints:
52+
- DisableAutoMapping: ~
53+
54+
.. code-block:: xml
55+
56+
<!-- config/validator/validation.xml -->
57+
<?xml version="1.0" encoding="UTF-8" ?>
58+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
59+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
60+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
61+
62+
<class name="App\Entity\BookCollection">
63+
<constraint name="DisableAutoMapping"/>
64+
</class>
65+
</constraint-mapping>
66+
67+
.. code-block:: php
68+
69+
// src/Entity/BookCollection.php
70+
namespace App\Entity;
71+
72+
use Symfony\Component\Validator\Constraints as Assert;
73+
use Symfony\Component\Validator\Mapping\ClassMetadata;
74+
75+
class BookCollection
76+
{
77+
// ...
78+
79+
public static function loadValidatorMetadata(ClassMetadata $metadata)
80+
{
81+
$metadata->addConstraint(new Assert\DisableAutoMapping());
82+
}
83+
}
84+
85+
Options
86+
-------
87+
88+
The ``groups`` option is not available for this constraint.
89+
90+
.. include:: /reference/constraints/_payload-option.rst.inc
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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-attributes
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+
#[Assert\EnableAutoMapping]
36+
class BookCollection
37+
{
38+
#[ORM\Column(nullable: false)]
39+
protected string $name = '';
40+
41+
#[ORM\ManyToOne(targetEntity: Author::class)]
42+
public Author $author;
43+
44+
// ...
45+
}
46+
47+
.. code-block:: yaml
48+
49+
# config/validator/validation.yaml
50+
App\Entity\BookCollection:
51+
constraints:
52+
- EnableAutoMapping: ~
53+
54+
.. code-block:: xml
55+
56+
<!-- config/validator/validation.xml -->
57+
<?xml version="1.0" encoding="UTF-8" ?>
58+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
59+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
60+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
61+
62+
<class name="App\Entity\BookCollection">
63+
<constraint name="EnableAutoMapping"/>
64+
</class>
65+
</constraint-mapping>
66+
67+
.. code-block:: php
68+
69+
// src/Entity/BookCollection.php
70+
namespace App\Entity;
71+
72+
use Symfony\Component\Validator\Constraints as Assert;
73+
use Symfony\Component\Validator\Mapping\ClassMetadata;
74+
75+
class BookCollection
76+
{
77+
// ...
78+
79+
public static function loadValidatorMetadata(ClassMetadata $metadata)
80+
{
81+
$metadata->addConstraint(new Assert\EnableAutoMapping());
82+
}
83+
}
84+
85+
Options
86+
-------
87+
88+
The ``groups`` option is not available for this constraint.
89+
90+
.. 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
@@ -104,3 +104,5 @@ Other Constraints
104104
* :doc:`Collection </reference/constraints/Collection>`
105105
* :doc:`Count </reference/constraints/Count>`
106106
* :doc:`UniqueEntity </reference/constraints/UniqueEntity>`
107+
* :doc:`EnableAutoMapping </reference/constraints/EnableAutoMapping>`
108+
* :doc:`DisableAutoMapping </reference/constraints/DisableAutoMapping>`

0 commit comments

Comments
 (0)
0