Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit87d48fa

Browse files
committed
minor#11132 [Validator][Doctrine] Add docs for automatic validation (dunglas)
This PR was squashed before being merged into the 4.3 branch (closes#11132).Discussion----------[Validator][Doctrine] Add docs for automatic validationSeesymfony/symfony#27735Commits-------d5e3496 [Validator][Doctrine] Add docs for automatic validation
2 parents403ad61 +d5e3496 commit87d48fa

File tree

1 file changed

+73
-2
lines changed

1 file changed

+73
-2
lines changed

‎doctrine.rst‎

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,9 @@ and save it::
353353
class ProductController extends AbstractController
354354
{
355355
/**
356-
* @Route("/product", name="product")
356+
* @Route("/product", name="create_product")
357357
*/
358-
public functionindex()
358+
public functioncreateProduct(): Response
359359
{
360360
// you can fetch the EntityManager via $this->getDoctrine()
361361
// or you can add an argument to your action: index(EntityManagerInterface $entityManager)
@@ -418,6 +418,76 @@ Take a look at the previous example in more detail:
418418
Whether you're creating or updating objects, the workflow is always the same: Doctrine
419419
is smart enough to know if it should INSERT or UPDATE your entity.
420420

421+
Validating Objects
422+
------------------
423+
424+
:doc:`The Symfony validator</validation>` reuses Doctrine metadata
425+
to perform some basic validation tasks::
426+
427+
// src/Controller/ProductController.php
428+
namespace App\Controller;
429+
430+
// ...
431+
use Symfony\Component\HttpFoundation\Response;
432+
use Symfony\Component\Validator\Validator\ValidatorInterface;
433+
434+
use App\Entity\Product;
435+
436+
class ProductController extends AbstractController
437+
{
438+
/**
439+
* @Route("/product", name="create_product")
440+
*/
441+
public function createProduct(ValidatorInterface $validator): Response
442+
{
443+
$product = new Product();
444+
$product->setName(null); // The column in database isn't nullable
445+
$product->setPrice('1999'); // Type mismatch, an integer is expected
446+
447+
// ...
448+
449+
$errors = $validator->validate($product);
450+
if (count($errors) > 0) {
451+
return new Response((string) $errors, 400);
452+
}
453+
454+
// Will not be reached in this example
455+
$entityManager = $this->getDoctrine()->getManager();
456+
$entityManager->persist($product);
457+
$entityManager->flush();
458+
459+
return new Response('Saved new product with id '.$product->getId());
460+
}
461+
}
462+
463+
The following table summarizes the mapping between Doctrine metadata and
464+
the corresponding validation constraints:
465+
466+
+--------------------+-----------------------------------------------------------+-------------------------------------------------------------------------+
467+
| Doctrine attribute| Validation constraint| Notes|
468+
+====================+===========================================================+=========================================================================+
469+
| ``nullable=true``|:doc:`NotNull</reference/constraints/NotNull>`| Relies on:doc:`the PropertyInfo component</components/property_info>`|
470+
+--------------------+-----------------------------------------------------------+-------------------------------------------------------------------------+
471+
| ``type``|:doc:`Type</reference/constraints/Type>`| Relies on:doc:`the PropertyInfo component</components/property_info>`|
472+
+--------------------+-----------------------------------------------------------+-------------------------------------------------------------------------+
473+
| ``unique=true``|:doc:`UniqueEntity</reference/constraints/UniqueEntity>`||
474+
+--------------------+-----------------------------------------------------------+-------------------------------------------------------------------------+
475+
| ``length``|:doc:`Length</reference/constraints/Length>`||
476+
+--------------------+-----------------------------------------------------------+-------------------------------------------------------------------------+
477+
478+
Because:doc:`the Form component</forms>` as well as `API Platform`_
479+
internally use the Validator Component, all your forms
480+
and web APIs will also automatically benefit from these default constraints.
481+
482+
..versionadded::4.3
483+
484+
The automatic validation has been added in Symfony 4.3.
485+
486+
..tip::
487+
488+
Don't forget to add:doc:`more precise validation constraints</reference/constraints>`
489+
to ensure that data provided by the user is correct.
490+
421491
Fetching Objects from the Database
422492
----------------------------------
423493

@@ -816,3 +886,4 @@ Learn more
816886
.. _`ParamConverter`:http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
817887
.. _`limit of 767 bytes for the index key prefix`:https://dev.mysql.com/doc/refman/5.6/en/innodb-restrictions.html
818888
.. _`Doctrine screencast series`:https://symfonycasts.com/screencast/symfony-doctrine
889+
.. _`API Platform`:https://api-platform.com/docs/core/validation/

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp