Symfony2 - Call EmailValidator inside a custom validator

I create a special validator constraint to check for "contact", something like "John Doe < jdoe@example.com >". Following the Cookbook , I created the Constraint class:

<?php namespace MyCompany\MyBundle\Validator\Constraints; use Symfony\Component\Validator\Constraint; /** * @Annotation */ class Contact extends Constraint { public $message = 'The string "%string%" is not a valid Contact.'; } 

and also created a validator:

 <?php namespace MyCompany\MyBundle\Validator\Constraints; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Constraints\Email; use Symfony\Component\Validator\Constraints\EmailValidator; class ContactValidator extends ConstraintValidator { public function validate($value, Constraint $constraint) { if (!preg_match('#(.*)\s+<(.*)>#', $value, $matches)) { $this->context->addViolation($constraint->message, array('%string%' => $value)); } $emailValidator = new EmailValidator(); if (isset($matches[2]) && $emailValidator->validate($matches[2], new Email())) { $this->context->addViolation($constraint->message, array('%string%' => $value)); } } } 

The fact is that I'm trying to use Symfony EmailValidator inside my custom validator to verify that the email is valid. I do not want to reinvent the wheel and check emails using my own regular expression.

Everything is fine when trying to confirm a valid contact, but checking the contact with an invalid email address ("Gabriel Garcia <infoinv4l1d3mai1.com>"), it fails with a PHP Fatal error:

Fatal error: call addViolation () member function for non-object in /home/dev/myproject/vendor/symfony/symfony/src/Symfony/Component/Validator/Constraints/EmailValidator.php on line 58

Delving into the EmailValidator.php class, I realized that the problem was with $ context (ExecutionContext). Here is line 58 of EmailValidator.php:

 $this->context->addViolation($constraint->message, array('{{ value }}' => $value)); 

It seems that the class context attribute is null. Does anyone know why? Do I need to enter something?

Thanks in advance.

PS: I am using Symfony 2.3. Ignore the regex, I know it can be much better. It is just for testing right now.

+4
source share
3 answers

You can directly use the restriction

see http://symfony.com/doc/current/book/validation.html

 use Symfony\Component\Validator\Constraints\Email $emailConstraint = new Email(); // use the validator to validate the value $errorList = $this->get('validator')->validateValue( $email, $emailConstraint ); 

Yours faithfully,

+3
source

I think the original question was to use EmailValidator inside Custom Validator, and the container is not available in this scenario, so

 $this->get('validator'); 

will not work. It seems that the only problem the poster was to add the EmailValidator addViolation to the right context. This should work:

 $emailValidator = new EmailValidator(); $emailValidator->initialize($this->context); $emailValidator->validate($matches[2], $constraint); 
+4
source

Finding this question after you tried to invoke a custom internal custom, I did some deep research, and I could just find another better way (easier for me).

Allowed: Sf2.6> =

 $this->context->getValidator() ->inContext($this->context) ->atPath("time_on_point") ->validate($timeOnPoint, new AssertCustom\DateTimeRange(array('min' => '+1 day'))); 

In this case, I declared a new custom validator, for example a class-specific validator , and I could go directly to the field by its name. The advantage of this: I can name another custom, using only the “new AssertCustom”, and if this “AssertCustom” needs a service such as a constructor, I will have no dependency, because the configuration service will broadcast all the content transparently.

Be careful if you invoke a recursively (deep) field, you will need to adapt the context according to the comments found in this file: Symfony \ Component \ Validator \ Constraints \ CollectionValidator

+1
source

Source: https://habr.com/ru/post/1488432/


All Articles