[英]Zend Framework 2 - How to set form validation rules for an entity using annotations?
如何為電子郵件地址設置@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'
通過使用@uniqueConstraint,請參見此處:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.