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



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

/**
 * @var string
 *
 * @ORMColumn(name="email", type="string", unique=true, length=255, nullable=false)
 * @AnnotationType("ZendFormElementEmail")
 * @AnnotationValidator({"name":"EmailAddress", "options": {"unique":"true"}})
 * @AnnotationErrorMessage("Invalid Email Address")
 * @AnnotationAttributes({"placeholder":"Your Email Address"})
 * @AnnotationOptions({"label":"Your email address:"})
 */
public $email;

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

DoctrineDBALDBALException
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'

通过使用@uniqueConstraint,请参阅此处:

http://docs.doctrine-project.org/en/2.0.x/reference/annotations-reference.html#annref-uniqueconstraint

相关内容

  • 没有找到相关文章