조금 늦었지만 가능한 답변 일 수 있습니다.
날짜 요소 예 :
나는 같은 폼 클래스의 메소드를 호출하는 사용자 정의 콜백 검증과 날짜 요소를 만들었습니다. 당신이 그것을 사용자 지정 유효성 검사기와 함께 자신의 클래스가있는 경우
// 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
안녕하세요 @ 리야 리 저는 젠드 폼 추상을 확장 한 클래스 안에 있습니다. 그 값 $ username을 어떻게 얻을 수 있습니까? 어리석은 질문이라면 미안 해요. –
무엇을 의미합니까? 나는 당신이 Zend_Form에 대해 이야기하고 있다고 생각합니다. '$ username'은 단순히 현재 사용자의 사용자 이름입니다. 생성자를 사용하여 양식에 전달할 수 있습니다. 사용자 존재 여부를 확인하려는 경우이 옵션을 사용할 필요가 없습니다. – Liyali
새 사용자를 추가하고 있습니다. 확인. 나는 ($ form-> isValid ($ request-> getPost()) 경우에만 사용된다.)이 isvalid 메서드는 모두를 처리한다. 그래서 어떻게 사용자 폼 클래스를 추가하는 사용자 이름 값을 입력 할 수 있습니다.$ this-> getValue ('username')를 사용하여 얻을 수 있습니까? –