Zend Framework 2 - 如何使用注释为实体设置表单验证规则?

时间:2014-02-08 16:13:36

标签: php validation doctrine-orm annotations zend-framework2

如何为电子邮件地址设置@Annotation \ Validator以确保它是唯一的。

/**
 * @var string
 *
 * @ORM\Column(name="email", type="string", unique=true, length=255, nullable=false)
 * @Annotation\Type("Zend\Form\Element\Email")
 * @Annotation\Validator({"name":"EmailAddress", "options": {"unique":"true"}})
 * @Annotation\ErrorMessage("Invalid Email Address")
 * @Annotation\Attributes({"placeholder":"Your Email Address"})
 * @Annotation\Options({"label":"Your email address:"})
 */
public $email;

如果电子邮件的格式错误或字符无效,它确实会验证电子邮件并显示错误消息但如果电子邮件重复,则会出现以下错误:

Doctrine\DBAL\DBALException

File:
doctrine/dbal/lib/Doctrine/DBAL/DBALException.php:91
Message:
An exception occurred while executing 'INSERT INTO subscriber (email, token, active) VALUES (?, ?, ?)' with params ["me@example.com", {}, 1]:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'me@example.com' for key 'email'

1 个答案:

答案 0 :(得分:0)