1

Symfony2当我在框架中提交表单时,我尝试使用正则表达式验证电子邮件地址。我在类中使用checkMX and checkHost了属性。Entity这完成了。但我无法验证它的正则表达式, {,$,#,!,^......请帮助某人......这是我的代码片段......

<?php

namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * product1
 */
class product1
{
    /**
     * @var int
     */
    private $id;


    /**
     * @var string
     * @Assert\Email(
     *
     *     checkHost= true,
     *     checkMX = true,
     *     message="Invalid email host"
     *
     *
     * )
     * @Assert\Email(
     *     strict=false,
     *     message="invalid"
     * )
     *
     */
    private $email;

    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set email
     *
     * @param string $email
     *
     * @return product1
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

    /**
     * Get email
     *
     * @return string
     */
    public function getEmail()
    {
        return $this->email;
    }
}
4

2 回答 2

4

根据文档,您可以使用此注释:

/**
 * @Assert\Regex(
 *     pattern="/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/",
 *     message="not_valid_email"
 * )
 *
 * @var string
 */
private $email;
于 2016-08-26T07:41:47.263 回答
0

http://symfony.com/doc/current/reference/constraints/Email.html

我认为@Assert\Email 可以完成这项工作,但如果您想实现另外一个定制的正则表达式,您可以使用 Assert 标签。

 * @Assert\Regex(
 *     pattern = "/^[a-zA-Z0-9\-\_]+$/",
 *     message = "invalid characters"
 * )

我知道这个正则表达式不是你想要的,但我认为这里使用的格式与任何其他语言中使用的其他常规格式没有太大区别(某些字符需要用反斜杠转义)。

于 2016-08-26T04:00:49.580 回答