Symfony2 - 在自定义验证器中调用 EmailValidator [英] Symfony2 - Call EmailValidator inside a Custom Validator

查看:33
本文介绍了Symfony2 - 在自定义验证器中调用 EmailValidator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个自定义验证器约束来验证联系人",类似于John Doe <jdoe@example.com>".按照 Cookbook 我创建了约束类:

I'm creating a custom validator constraint to validate a "Contact", which is something like "John Doe <jdoe@example.com>". Following the Cookbook I've 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.';
}

并且还创建了验证器:

<?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));    
        }
    }
}

关键是我试图在我的自定义验证器中使用 Symfony 的 EmailValidator 来检查电子邮件是否有效.我不想重新发明轮子并使用我自己的正则表达式验证电子邮件.

The point is that I'm trying to use the Symfony's EmailValidator inside my custom validator to check the email is valid. I don't want to reinvent the wheel and validate the email using my own regex.

尝试验证有效联系人时一切正常,但使用无效电子邮件(Gabriel Garcia <infoinv4l1d3mai1.com>")测试联系人时会出现 PHP 致命错误:

Everything is ok when trying to validate a valid contact but, testing a contact with invalid email ("Gabriel Garcia <infoinv4l1d3mai1.com>") it craches with a PHP Fatal error:

致命错误:在第 58 行的/home/dev/myproject/vendor/symfony/symfony/src/Symfony/Component/Validator/Constraints/EmailValidator.php 中的非对象上调用成员函数 addViolation()

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

深入研究EmailValidator.php 类,我意识到问题与$context (ExecutionContext) 有关.这是 EmailValidator.php 的第 58 行:

Digging into the EmailValidator.php class, I've realized that the issue is related to the $context (ExecutionContext). Here is line 58 of EmailValidator.php:

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

似乎该类的上下文属性为空.有谁知道为什么?我需要在某处注射吗?

Seems that context attribute of the class is null. Anyone knows why? I need to inject it somewhere?

提前致谢.

PS:我使用的是 Symfony 2.3.不要关注正则表达式,我知道它可以好得多.现在只是为了测试.

P.S.: I'm using Symfony 2.3. Do not pay attention to the regex, I know it can be so much better. It's just for testing right now.

推荐答案

可以直接使用约束

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
);

最好的问候

这篇关于Symfony2 - 在自定义验证器中调用 EmailValidator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆