2010-07-07 131 views
1

我想弄清楚如何让这个zend表单来验证。我不明白:无法弄清楚如何验证zend_form

是addValidator()参数特定的验证器?在那些验证器的某处是否有列表?

我在表格/ contact.php得到这个:

类Application_Form_Contact扩展Zend_Form的 {

public function init() 
{ 
    $this->setAction('index/process'); 
    $this->setMethod('post'); 

    $name = new Zend_Form_Element_Text('name'); 
    $name->setLabel('Name:'); 
// $name->addValidator('alnum'); 
    $name->setRequired(true); 

    $email = new Zend_Form_Element_Text('email'); 
    $email->setLabel('Email:')->setRequired(true); 

    $confirm = new Zend_Form_Element_Text('confirm'); 
    $confirm->setLabel('Confirm Email:')->setRequired(true); 

    $phone = new Zend_Form_Element_Text('phone'); 
    $phone->setLabel('Phone:')->setRequired(true); 

    $subject = new Zend_Form_Element_Select('subject'); 
    $subject->setLabel('Subject:')->setRequired(true); 
    $subject->setMultiOptions(array('Performance'=>'Performance', 
            'Workshop'=>'Workshop', 
            'Other'=>'Other' 
           )); 


    $message = new Zend_Form_Element_Textarea('message'); 
    $message->setLabel('Message:')->setRequired(true); 
    $message->setAttrib('rows','6'); 
    $message->setAttrib('cols','30'); 

    $submit = new Zend_Form_Element_Submit('Submit'); 



    $this->addElements(array( $name, 
        $email, 
        $confirm, 
        $phone, 
        $subject, 
        $message, 
        $submit 
)); 


      $this->setElementDecorators(array 
     ('ViewHelper', 

     array(array('data' => 'HtmlTag'), array('tag' => 'td')), 
     array('Label' , array('tag' => 'td')), 
     array(array('row' => 'HtmlTag') , array('tag' => 'tr')) 

     )); 

    $submit->setDecorators(array('ViewHelper', 

     array(array('data' => 'HtmlTag'), array('tag' => 'td')), 
     array(array('emptyrow' => 'HtmlTag'), array('tag' => 'td', 'placement' => 'PREPEND')), 
     array(array('row' => 'HtmlTag') , array('tag' => 'tr')) 
     ));     

    $this->setDecorators(array(
     'FormElements', 

     array('HtmlTag' , array('tag' => 'table' , 'class' => 'formTable')), 
     'Form' 
    ) 
    ); 
} 
} 

我的控制器:

public function indexAction() 
{ 

    $this->view->form = new Application_Form_Contact(); 

} 

public function processAction() 
{ 
//    $this->view->form = new Application_Form_Contact(); 
//  
    if ($this->_request->isPost()) { 
       $formData = $this->_request->getPost(); 

      // echo 'success'; 
       $this->view->data = $formdata; 

      } else { 
     //  $form->populate($formData); 
      } 


} 

我是个新手,所以我可能会犯一些我看不到的明显错误。我试图做基本的验证:

  • 所有字段都必须填写
  • 所有的HTML被剥夺
  • 电子邮件并确认
  • 电子邮件字段必须
  • 电子邮件匹配必须是有效的格式。

任何帮助将不胜感激!

回答

5

您是否尝试过的isValid():

$form = new forms_ContactForm(); 

    if ($this->_request->isPost()) { 
     $formData = $this->_request->getPost(); 
     if ($form->isValid($formData)) { 
      echo 'success'; 
      exit; 
     } else { 
      $form->populate($formData); 
     } 
    } 

    $this->view->form = $form; 

from

关于验证:

$firstName = new Zend_Form_Element_Text('firstName'); 
    $firstName->setLabel('First name') 
       ->setRequired(true) 
       ->addValidator('NotEmpty'); 

    $lastName = new Zend_Form_Element_Text('lastName'); 
    $lastName->setLabel('Last name') 
      ->setRequired(true) 
      ->addValidator('NotEmpty'); 

    $email = new Zend_Form_Element_Text('email'); 
    $email->setLabel('Email address') 
      ->addFilter('StringToLower') 
      ->setRequired(true) 
      ->addValidator('NotEmpty', true) 
      ->addValidator('EmailAddress'); 

继承人的链接有关的Zend fiorms和验证了Zend文档。 Creating Form Elements Using Zend_Form_Element

+0

好吧,如果我有适当的验证程序,那么它可能知道它是否有效,但我不知道如何使用addValidators() – Joel 2010-07-08 04:05:21

+0

@Iznogood很好,谢谢!你能指出我的验证者列表吗?我一直在寻找高和低。 – Joel 2010-07-08 04:13:57

+0

@Joel再次编辑它。从那里你总是可以搜索谷歌的Zend表格验证器,并从那里继续。那里有很多信息。 – Iznogood 2010-07-08 04:17:38