implement symfony/constraint/validation as assert in laravel/doctrine

Viewed 1095

For a new project, I am using Laravel/Doctrine.

All works well, I implement the Auth Services too.

I use symfony a lot, so I was wondering if there is a way to implement the Symfony validations constraints for doctrine in laravel, or anything else like this symfony component in Laravel.

In order to use in my doctrine entities like this:

use Symfony\Component\Validator\Constraints as Assert;

class Test
{
    /**
     * @Assert\Email(
     *     message = "The email '{{ value }}' is not a valid email.",
     *     checkMX = true
     * )
     */
     protected $email;
}

Is there a way to show me how to make this in the good practices?

2 Answers

composer.json

    "symfony/validator": "^5.1",
    "laravel-doctrine/orm": "~1.5",
    "doctrine/annotations": "^1.11",

public/index.php

use Doctrine\Common\Annotations\AnnotationRegistry;

$loader = require __DIR__.'/../vendor/autoload.php';

AnnotationRegistry::registerLoader(array($loader, "loadClass"));

for correct works console doctrine tools need too add in

artisan

use Doctrine\Common\Annotations\AnnotationRegistry;
$loader = require __DIR__.'/vendor/autoload.php';
AnnotationRegistry::registerLoader(array($loader, "loadClass"));

the you can use validator for validate entity by the Symfony\Component\Validator\Constraints

Related