Hello,

I created custom symfony validator and constraint for standalone use, not 
framework. I wrote this code to inject doctrine entity manager to validator 
class.

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Validator\ContainerConstraintValidatorFactory;

$container = new ContainerBuilder();
$container
    ->register('customEmailUniqueEntity', 'EmailExistsValidator')
    ->addArgument($em);
$validatorBuilder = Validation::createValidatorBuilder();
$validatorBuilder->setConstraintValidatorFactory(
    new ContainerConstraintValidatorFactory($container)
);

$validator = $validatorBuilder->getValidator();
$violations = $validator->validate('email address', [ 
    new EmailExists() 
]);

if (0 !== count($violations)) {
    // there are errors, now you can show them
    foreach ($violations as $violation) {
        echo $violation->getMessage().'<br>';
    }
}

With this code both dependency injection and validation/constraint works fine, 
but is there a trick to have this custom constraint as 'constraint' array 
argument within form builder rather than validating it manually as above?

->add('email', EmailType::class, [                                              
    'constraints' => [
        new Assert\Email(['message' => 'invalid.email', 'mode' => 'strict', 
'normalizer' => 'trim']),
        new Assert\EmailExists($em),
    ],
 ]);

With code above I cannot pass $em to the constructor of my custom Validator. 
Any trick possible? Is here someone who used doctrine with standalone symfony 
form/validator?

-- 
You received this message because you are subscribed to the Google Groups 
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/doctrine-user/80791074-24f9-4fc4-bb81-b03d83dc9e2a%40googlegroups.com.

Reply via email to