8000 minor #18505 [Validator] [Validation] Document the auto_mapping confi… · symfony/symfony-docs@c2c22eb · GitHub
[go: up one dir, main page]

Skip to content

Commit c2c22eb

Browse files
committed
minor #18505 [Validator] [Validation] Document the auto_mapping config 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
2 parents be073b5 + 07ac755 commit c2c22eb

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
@@ -2508,6 +2508,65 @@ enabled
25082508
validation
25092509
~~~~~~~~~~
25102510

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

25132572
enabled

0 commit comments

Comments
 (0)
0