Skip to content

[Validator] Add EnableAutoMapping and DisableAutoMapping constraints #18568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions reference/configuration/doctrine.rst
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ This option is ``false`` by default and it's considered a legacy option. It was
only useful in previous Symfony versions, when it was recommended to use bundles
to organize the application code.

.. _doctrine_auto-mapping:

Custom Mapping Entities in a Bundle
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
2 changes: 2 additions & 0 deletions reference/constraints.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ Validation Constraints Reference
constraints/Traverse
constraints/CssColor
constraints/Cascade
constraints/EnableAutoMapping
constraints/DisableAutoMapping

The Validator is designed to validate objects against *constraints*.
In real life, a constraint could be: "The cake must not be burned". In
Expand Down
119 changes: 119 additions & 0 deletions reference/constraints/DisableAutoMapping.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
DisableAutoMapping
==================

This constraint allows to disable Doctrine's ``auto_mapping`` on a class or a
property. You can read more about it
:ref:`here <doctrine_auto-mapping>`. Automapping allows to determine
validation rules based on Doctrine's annotations and attributes. You may
use this constraint when automapping is globally enabled, but you still want to
disable this feature for a class or a property specifically.

========== ===================================================================
Applies to :ref:`property or method <validation-property-target>`
Class :class:`Symfony\\Component\\Validator\\Constraints\\DisableAutoMapping`
========== ===================================================================

Basic Usage
-----------

In the following example, the
:class:`Symfony\\Component\\Validator\\Constraints\\DisableAutoMapping`
constraint will tell the validator to not gather constraints from Doctrine's
metadata:

.. configuration-block::

.. code-block:: php-annotations

// src/Model/BookCollection.php
namespace App\Model;

use App\Model\Author;
use App\Model\BookMetadata;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @Assert\DisableAutoMapping
*/
class BookCollection
{
/**
* @ORM\Column(nullable=false)
*/
protected $name = '';

/**
* @ORM\ManyToOne(targetEntity=Author::class)
*/
public Author $author;

// ...
}

.. code-block:: php-attributes

// src/Model/BookCollection.php
namespace App\Model;

use App\Model\Author;
use App\Model\BookMetadata;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

#[Assert\DisableAutoMapping]
class BookCollection
{
#[ORM\Column(nullable: false)]
protected string $name = '';

#[ORM\ManyToOne(targetEntity: Author::class)]
public Author $author;

// ...
}

.. code-block:: yaml

# config/validator/validation.yaml
App\Entity\BookCollection:
constraints:
- DisableAutoMapping: ~

.. code-block:: xml

<!-- config/validator/validation.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="App\Entity\BookCollection">
<constraint name="DisableAutoMapping"/>
</class>
</constraint-mapping>

.. code-block:: php

// src/Entity/BookCollection.php
namespace App\Entity;

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Mapping\ClassMetadata;

class BookCollection
{
// ...

public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addConstraint(new Assert\DisableAutoMapping());
}
}

Options
-------

The ``groups`` option is not available for this constraint.

.. include:: /reference/constraints/_payload-option.rst.inc
119 changes: 119 additions & 0 deletions reference/constraints/EnableAutoMapping.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
EnableAutoMapping
=================

This constraint allows to enable Doctrine's ``auto_mapping`` on a class or a
property. You can read more about it
:ref:`here <doctrine_auto-mapping>`. Automapping allows to determine
validation rules based on Doctrine's annotations and attributes. You may
use this constraint when automapping is globally disabled, but you still want
to enable this feature for a class or a property specifically.

========== ===================================================================
Applies to :ref:`property or method <validation-property-target>`
Class :class:`Symfony\\Component\\Validator\\Constraints\\EnableAutoMapping`
========== ===================================================================

Basic Usage
-----------

In the following example, the
:class:`Symfony\\Component\\Validator\\Constraints\\EnableAutoMapping`
constraint will tell the validator to gather constraints from Doctrine's
metadata:

.. configuration-block::

.. code-block:: php-annotations

// src/Model/BookCollection.php
namespace App\Model;

use App\Model\Author;
use App\Model\BookMetadata;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @Assert\EnableAutoMapping
*/
class BookCollection
{
/**
* @ORM\Column(nullable=false)
*/
protected $name = '';

/**
* @ORM\ManyToOne(targetEntity=Author::class)
*/
public Author $author;

// ...
}

.. code-block:: php-attributes

// src/Model/BookCollection.php
namespace App\Model;

use App\Model\Author;
use App\Model\BookMetadata;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

#[Assert\EnableAutoMapping]
class BookCollection
{
#[ORM\Column(nullable: false)]
protected string $name = '';

#[ORM\ManyToOne(targetEntity: Author::class)]
public Author $author;

// ...
}

.. code-block:: yaml

# config/validator/validation.yaml
App\Entity\BookCollection:
constraints:
- EnableAutoMapping: ~

.. code-block:: xml

<!-- config/validator/validation.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="App\Entity\BookCollection">
<constraint name="EnableAutoMapping"/>
</class>
</constraint-mapping>

.. code-block:: php

// src/Entity/BookCollection.php
namespace App\Entity;

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Mapping\ClassMetadata;

class BookCollection
{
// ...

public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addConstraint(new Assert\EnableAutoMapping());
}
}

Options
-------

The ``groups`` option is not available for this constraint.

.. include:: /reference/constraints/_payload-option.rst.inc
2 changes: 2 additions & 0 deletions reference/constraints/map.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,5 @@ Other Constraints
* :doc:`Collection </reference/constraints/Collection>`
* :doc:`Count </reference/constraints/Count>`
* :doc:`UniqueEntity </reference/constraints/UniqueEntity>`
* :doc:`EnableAutoMapping </reference/constraints/EnableAutoMapping>`
* :doc:`DisableAutoMapping </reference/constraints/DisableAutoMapping>`