有點晚了,但這可能是你的問題的一個可能的答案。
日期元素例如:
我創建日期元件與調用相同形式類的方法的自定義回調驗證器。
// Date picker from
$datePickerFrom = new Zend_Form_Element_Text('date_picker_from_date');
$datePickerFrom->setRequired(true)
->setDecorators($this->elementDecorators)
->setLabel('Valid From')
->addFilter('StripTags')
->addFilter('StringTrim')
->setValue(date("d/m/Y",strtotime(Zend_Date::now())))
->addValidator('Callback',true, array('callback' => array($this, 'dateCheck'));
$datePickerFrom->getValidator('Callback')->setMessage("'%value%' Cannot be in the past.");
$this->addElement($datePickerFrom);
如果你有自己的類,在它的自定義驗證器,那麼你可以這樣做:
$myValidatorClass = new \SomeValidaotrClass();
->addValidator('Callback',true, array('callback' => array($myValidatorClass, 'dateCheck'));
在我創建的自定義驗證方法相同的窗體類:
public function dateCheck($value, $elements, $option)
{
$now = new DateTime();
$fromDate = new DateTime(str_replace('/', '-', $elements['date_picker_from_date']));
if ($fromDate < $now) {
$this->getElement('date_picker_from_date')->getValidator('Callback')->setMessage("'%value%' Cannot be in the past.");
return false;
} else (SomethingElse) {
$this->getElement('date_picker_from_date')->getValidator('Callback')->setMessage("Some other message.");
}
return true;
}
或者如果您使用自己的班級:
class SomeValidaotrClass
{
public function dateCheck($value, $elements, $option)
{
// Validation code...
}
}
如果方法不是public
回調將不會找到它們。
來源
2014-12-17 11:09:49
Kal
嘿@Liyali我在擴展zend窗體抽象類的內部。我怎樣才能得到這個值$用戶名?對不起,如果傻問題。 – 2012-02-28 13:28:16
你是什麼意思?我想你在談論Zend_Form。 '$ username'只是當前用戶的用戶名,你可以使用它的構造函數將它傳遞給你的表單。請注意,如果您只是試圖檢查用戶是否存在,則不需要使用它。 – Liyali 2012-02-28 13:33:50
請參閱我正在添加新用戶。我只使用如果($ form-> isValid($ request-> getPost()))全部由此isvalid方法處理。那麼我怎樣才能得到輸入的用戶名值呢,它添加了用戶表單類。我可以通過使用$ this-> getValue('username')來獲得它 – 2012-02-28 13:43:40