사용자 정의 유효성 검사기를 작성하고 제공된 $context
변수를 사용합니다.
짧은 예
컨트롤러
class MyController extends Zend_Controller_Action {
public function indexAction() {
$form = new Application_Form_Gender();
$this->view->form = $form;
if ($this->getRequest()->isPost()) {
if ($form->isValid($this->getRequest()->getPost())) {
/*...*/
}
}
}
}
형태
class Application_Form_Gender extends Zend_Form {
public function init()
{
$this->addElement('radio', 'radio1', array('multiOptions' => array('m' => 'male', 'w' => 'female')));
$this->getElement('radio1')->isRequired(true);
$this->getElement('radio1')->setAllowEmpty(false);
$this->addElement('text', 'textm', array('label' => 'If you are male enter something here');
$this->getElement('textm')->setAllowEmpty(false)->addValidator(new MyValidator('m'));
$this->addElement('text', 'textf', array('label' => 'If you are female enter something here'));
$this->getElement('textf')->setAllowEmpty(false)->addValidator(new MyValidator('f'));
$this->addElement('submit', 'submit');
}
검사기
01,230,
이 예에서 알 수 있듯이 $form->isValid()
에 제공된 모든 데이터는 $context
변수를 통해 제공되며 원하는 모든 검사를 수행 할 수 있습니다.
나를 위해 가장 편리한 'isValid'를 재정의했습니다. '$ context' 두 번째 인수를 사용하는 사용자 정의 유효성 검사기를 사용해 볼 수도 있습니다. 그러나 양식에 직접 액세스 할 필요가 없으므로 약간의 번거 로움이 있습니다. 때로는 편리 할 수도 있고 필요할 수도 있습니다. – bububaba