8000 [Validator] [Validation] Document the auto_mapping config option · symfony/symfony-docs@07ac755 · GitHub
[go: up one dir, main page]

Skip to content

Commit 07ac755

Browse files
committed
[Validator] [Validation] Document the auto_mapping config option
1 parent cae60d7 commit 07ac755

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

doctrine.rst

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,12 @@ is smart enough to know if it should INSERT or UPDATE your entity.
420420
Validating Objects
421421
------------------
422422

423-
:doc:`The Symfony validator </validation>` reuses Doctrine metadata to perform
424-
some basic validation tasks::
423+
:doc:`The Symfony validator </validation>` can reuse Doctrine metadata to perform
424+
some basic validation tasks. First, add or configure the
425+
:ref:`auto_mapping option <reference-validation-auto-mapping>` to define which
426+
entities should be introspected by Symfony to add automatic validation constraints.
427+
428+
Consider the following controller code::
425429

426430
// src/Controller/ProductController.php
427431
namespace App\Controller;
@@ -455,9 +459,11 @@ some basic validation tasks::
455459
}
456460

457461
Although the ``Product`` entity doesn't define any explicit
458-
:doc:`validation configuration </validation>`, Symfony introspects the Doctrine
459-
mapping configuration to infer some validation rules. For example, given that
460-
the ``name`` property can't be ``null`` in the database, a
462+
:doc:`validation configuration </validation>`, if the ``auto_mapping`` option
463+
includes it in the list of entities to introspect, Symfony will infer some
464+
validation rules for it and will apply them.
465+
466+
For example, given that the ``name`` property can't be ``null`` in the database, a
461467
:doc:`NotNull constraint </reference/constraints/NotNull>` is added automatically
462468
to the property (if it doesn't contain that constraint already).
463469

reference/configuration/framework.rst

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2499,6 +2499,65 @@ enabled
24992499
validation
25002500
~~~~~~~~~~
25012501

2502+
.. _reference-validation-auto-mapping:
2503+
2504+
auto_mapping
2505+
............
2506+
2507+
**type**: ``array`` **default**: ``[]``
2508+
2509+
Defines the Doctrine entities that will be introspected to add
2510+
:ref:`automatic validation constraints <automatic_object_validation>` to them:
2511+
2512+
.. configuration-block::
2513+
2514+
.. code-block:: yaml
2515+
2516+
framework:
2517+
validation:
2518+
auto_mapping:
2519+
# an empty array means that all entities that belong to that
2520+
# namespace will add automatic validation
2521+
'App\Entity\': []
2522+
'Foo\': ['Foo\Some\Entity', 'Foo\Another\Entity']
2523+
2524+
.. code-block:: xml
2525+
2526+
<!-- config/packages/framework.xml -->
2527+
<?xml version="1.0" encoding="UTF-8" ?>
2528+
<container xmlns="http://symfony.com/schema/dic/services"
2529+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2530+
xmlns:framework="http://symfony.com/schema/dic/symfony"
2531+
xsi:schemaLocation="http://symfony.com/schema/dic/services
2532+
https://symfony.com/schema/dic/services/services-1.0.xsd
2533+
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
2534+
2535+
<framework:config>
2536+
<framework:validation>
2537+
<framework:auto-mapping>
2538+
<framework:service namespace="App\Entity\"/>
2539+
2540+
<framework:service namespace="Foo\">Foo\Some\Entity</framework:service>
2541+
<framework:service namespace="Foo\">Foo\Another\Entity</framework:service>
2542+
</framework:auto-mapping>
2543+
</framework:validation>
2544+
</framework:config>
2545+
</container>
2546+
2547+
.. code-block:: php
2548+
2549+
// config/packages/framework.php
2550+
use Symfony\Config\FrameworkConfig;
2551+
2552+
return static function (FrameworkConfig $framework): void {
2553+
$framework->validation()
2554+
->autoMapping()
2555+
->paths([
2556+
'App\\Entity\\' => [],
2557+
'Foo\\' => ['Foo\\Some\\Entity', 'Foo\\Another\\Entity'],
2558+
]);
2559+
};
2560+
25022561
.. _reference-validation-enabled:
25032562

25042563
enabled

0 commit comments

Comments
 (0)
0