2012-07-31 2 views
1

다음과 같이 묻는 양식이 있다고 가정 해 보겠습니다. "당신은 Mr./Mrs입니까?" 답변 값에 따라 우리는 훨씬 더 검증 할 것입니다. 예를 들어요소/필드 값에 따라 Zend_Form에서 유효성 검사가 필요합니까?

, 씨는>

isValid 기능을 대체 할 됐나요 좋아하는 꽃의 유효성을 검사> 좋아하는 자동차 모델 부인의 유효성을 검사? 모범 사례의 예가 있을까요?

+1

나를 위해 가장 편리한 'isValid'를 재정의했습니다. '$ context' 두 번째 인수를 사용하는 사용자 정의 유효성 검사기를 사용해 볼 수도 있습니다. 그러나 양식에 직접 액세스 할 필요가 없으므로 약간의 번거 로움이 있습니다. 때로는 편리 할 수도 있고 필요할 수도 있습니다. – bububaba

답변

1

사용자 정의 유효성 검사기를 작성하고 제공된 $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 변수를 통해 제공되며 원하는 모든 검사를 수행 할 수 있습니다.