Skip to content

Commit

Permalink
minor #18505 [Validator] [Validation] Document the auto_mapping confi…
Browse files Browse the repository at this point in the history
…g option (javiereguiluz)

This PR was squashed before being merged into the 6.2 branch.

Discussion
----------

[Validator] [Validation] Document the auto_mapping config option

Fixes #18245.

Please, review the XML and PHP config example because I just made them up.

Commits
-------

07ac755 [Validator] [Validation] Document the auto_mapping config option
  • Loading branch information
javiereguiluz committed Jul 10, 2023
2 parents be073b5 + 07ac755 commit c2c22eb
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 5 deletions.
16 changes: 11 additions & 5 deletions doctrine.rst
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,12 @@ is smart enough to know if it should INSERT or UPDATE your entity.
Validating Objects
------------------

:doc:`The Symfony validator </validation>` reuses Doctrine metadata to perform
some basic validation tasks::
:doc:`The Symfony validator </validation>` can reuse Doctrine metadata to perform
some basic validation tasks. First, add or configure the
:ref:`auto_mapping option <reference-validation-auto-mapping>` to define which
entities should be introspected by Symfony to add automatic validation constraints.

Consider the following controller code::

// src/Controller/ProductController.php
namespace App\Controller;
Expand Down Expand Up @@ -455,9 +459,11 @@ some basic validation tasks::
}

Although the ``Product`` entity doesn't define any explicit
:doc:`validation configuration </validation>`, Symfony introspects the Doctrine
mapping configuration to infer some validation rules. For example, given that
the ``name`` property can't be ``null`` in the database, a
:doc:`validation configuration </validation>`, if the ``auto_mapping`` option
includes it in the list of entities to introspect, Symfony will infer some
validation rules for it and will apply them.

For example, given that the ``name`` property can't be ``null`` in the database, a
:doc:`NotNull constraint </reference/constraints/NotNull>` is added automatically
to the property (if it doesn't contain that constraint already).

Expand Down
59 changes: 59 additions & 0 deletions reference/configuration/framework.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2508,6 +2508,65 @@ enabled
validation
~~~~~~~~~~

.. _reference-validation-auto-mapping:

auto_mapping
............

**type**: ``array`` **default**: ``[]``

Defines the Doctrine entities that will be introspected to add
:ref:`automatic validation constraints <automatic_object_validation>` to them:

.. configuration-block::

.. code-block:: yaml
framework:
validation:
auto_mapping:
# an empty array means that all entities that belong to that
# namespace will add automatic validation
'App\Entity\': []
'Foo\': ['Foo\Some\Entity', 'Foo\Another\Entity']
.. code-block:: xml
<!-- config/packages/framework.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
<framework:config>
<framework:validation>
<framework:auto-mapping>
<framework:service namespace="App\Entity\"/>
<framework:service namespace="Foo\">Foo\Some\Entity</framework:service>
<framework:service namespace="Foo\">Foo\Another\Entity</framework:service>
</framework:auto-mapping>
</framework:validation>
</framework:config>
</container>
.. code-block:: php
// config/packages/framework.php
use Symfony\Config\FrameworkConfig;
return static function (FrameworkConfig $framework): void {
$framework->validation()
->autoMapping()
->paths([
'App\\Entity\\' => [],
'Foo\\' => ['Foo\\Some\\Entity', 'Foo\\Another\\Entity'],
]);
};
.. _reference-validation-enabled:

enabled
Expand Down

0 comments on commit c2c22eb

Please sign in to comment.