[英]Zend Framework: How to add a custom validation rule in Zend Form?
我創建了一個zend表單,可以很好地與驗證一起使用。
無論如何,我可以在此表單中為電子郵件地址添加自定義驗證,例如,我想設置驗證以檢查用戶提供的電子郵件地址是否為hotmail,因為我只接受hotmail電子郵件地址。
<?php
class Application_Form_RegistrationForm extends Zend_Form{
public function init(){
$firstname = $this->createElement('text', 'firstname');
$firstname->setLabel('Firstname: ')
->setRequired(true);
$lastname = $this->createElement('text', 'lastname');
$lastname->setLabel('Lastname: ')
->setRequired(true);
$email = $this->createElement('text', 'email_address');
$email->setLabel('Email Address: ')
->setRequired(true);
$register = $this->createElement('submit', 'register');
$register->setLabel('Create new Account')
->setIgnore(true);
$this->addElements(array(
$firstname, $lastname, $email, $register
));
}
}
?>
任何幫助將不勝感激。
class Mylib_Validate_UniqueEmail extends Zend_Validate_Abstract
{
const NOT_HOTMAIL = 'notHotmail';
protected $_messageTemplates = array(
self::NOT_HOTMAIL => "'%value%' is not a hotmail email address."
);
public function isValid($value)
{
$valueString = (string) $value;
$this->_setValue($valueString);
$email = explode('@',$value);
if (!substr_count($email[1],'hotmail') ) {
$this->_error(self:NOT_HOTMAIL);
return false;
}
return true;
}
}
這將是需要在表單中添加的validate類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.