4

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 2

1

I think doctrine doesn't even "know" about symfonys Validator Constraints. It is the DoctrineBundle glueing symfony/validator.

So maybe you could install the symfony/validator and do the glue code yourself.

Here is the DIC config that bind the validator to doctrine: https://github.com/doctrine/DoctrineBundle/blob/master/Resources/config/orm.xml#L47

Sign up to request clarification or add additional context in comments.

2 Comments

thank you for your answer, and indeed you're right: it is the DoctrineBundle glueing symfony/validator. I'll try your suggestion.
@mblaettermann, Can you provide additional information about how to solve problem? I added doctrine/doctrine-bundle via composer, but still have error. Or if I can solve problem without using doctrine/doctrine-bundle (it's more better for me) how can I do it? Thanks.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.