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

Appearance settings

Commit d5e3496

Browse filesBrowse files
dunglasjaviereguiluz
authored andcommitted
[Validator][Doctrine] Add docs for automatic validation
1 parent dd0bbc7 commit d5e3496
Copy full SHA for d5e3496

File tree

1 file changed

+73
-2
lines changed
Filter options

1 file changed

+73
-2
lines changed

‎doctrine.rst

Copy file name to clipboardExpand all lines: doctrine.rst
+73-2Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,9 @@ and save it!
347347
class ProductController extends AbstractController
348348
{
349349
/**
350-
* @Route("/product", name="product")
350+
* @Route("/product", name="create_product")
351351
*/
352-
public function index()
352+
public function createProduct(): Response
353353
{
354354
// you can fetch the EntityManager via $this->getDoctrine()
355355
// or you can add an argument to your action: index(EntityManagerInterface $entityManager)
@@ -412,6 +412,76 @@ Take a look at the previous example in more detail:
412412
Whether you're creating or updating objects, the workflow is always the same: Doctrine
413413
is smart enough to know if it should INSERT or UPDATE your entity.
414414

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

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.