@@ -353,12 +353,12 @@ and save it::
353353 class ProductController extends AbstractController
354354 {
355355 /**
356- * @Route("/product", name="product ")
356+ * @Route("/product", name="create_product ")
357357 */
358- public function index()
358+ public function createProduct(): Response
359359 {
360360 // you can fetch the EntityManager via $this->getDoctrine()
361- // or you can add an argument to your action: index (EntityManagerInterface $entityManager)
361+ // or you can add an argument to the action: createProduct (EntityManagerInterface $entityManager)
362362 $entityManager = $this->getDoctrine()->getManager();
363363
364364 $product = new Product();
@@ -418,6 +418,77 @@ Take a look at the previous example in more detail:
418418Whether you're creating or updating objects, the workflow is always the same: Doctrine
419419is 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 to perform
425+ 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+ // This will trigger an error: the column isn't nullable in the database
445+ $product->setName(null);
446+ // This will trigger a type mismatch error: an integer is expected
447+ $product->setPrice('1999');
448+
449+ // ...
450+
451+ $errors = $validator->validate($product);
452+ if (count($errors) > 0) {
453+ return new Response((string) $errors, 400);
454+ }
455+
456+ // ...
457+ }
458+ }
459+
460+ Although the ``Product `` entity doesn't define any explicit
461+ :doc: `validation configuration </validation >`, Symfony introspects the Doctrine
462+ mapping configuration to infer some validation rules. For example, given that
463+ the ``name `` property can't be ``null `` in the database, a
464+ :doc: `NotNull constraint </reference/constraints/NotNull >` is added automatically
465+ to the property (if it doesn't contain that constraint already).
466+
467+ The following table summarizes the mapping between Doctrine metadata and
468+ the corresponding validation constraints added automatically by Symfony:
469+
470+ ================== ========================================================= =====
471+ Doctrine attribute Validation constraint Notes
472+ ================== ========================================================= =====
473+ ``nullable=false `` :doc: `NotNull </reference/constraints/NotNull >` Requires installing the :doc: `PropertyInfo component </components/property_info >`
474+ ``type `` :doc: `Type </reference/constraints/Type >` Requires installing the :doc: `PropertyInfo component </components/property_info >`
475+ ``unique=true `` :doc: `UniqueEntity </reference/constraints/UniqueEntity >`
476+ ``length `` :doc: `Length </reference/constraints/Length >`
477+ ================== ========================================================= =====
478+
479+ Because :doc: `the Form component </forms >` as well as `API Platform `_ internally
480+ use the Validator component, all your forms and web APIs will also automatically
481+ benefit from these automatic validation constraints.
482+
483+ This automatic validation is a nice feature to improve your productivity, but it
484+ doesn't replace the validation configuration entirely. You still need to add
485+ some :doc: `validation constraints </reference/constraints >` to ensure that data
486+ provided by the user is correct.
487+
488+ .. versionadded :: 4.3
489+
490+ The automatic validation has been added in Symfony 4.3.
491+
421492Fetching Objects from the Database
422493----------------------------------
423494
@@ -816,3 +887,4 @@ Learn more
816887.. _`ParamConverter` : http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
817888.. _`limit of 767 bytes for the index key prefix` : https://dev.mysql.com/doc/refman/5.6/en/innodb-restrictions.html
818889.. _`Doctrine screencast series` : https://symfonycasts.com/screencast/symfony-doctrine
890+ .. _`API Platform` : https://api-platform.com/docs/core/validation/
0 commit comments