2010-08-28 113 views
1

Zend Form今天不是我的朋友!Zend Form不验证输入

这工作: -

控制器: -


public function indexAction() 
    { 
     $loginForm = new Application_Form_Login(); 

     //is there a submitted form? 
     if($this->getRequest()->isPost()){ 
     //yes there is so process it. 
     $formdata = $this->getRequest()->getPost(); 
     if($loginForm->isValid($formdata)){ 
      $user_logon = $loginForm->getValue('user_name'); 
      $user_pw = $loginForm->getValue('user_pw'); 
      if($this->authenticate($user_logon, $user_pw)){ 
      $this->_redirect(); 
      } 
     } else { 
      $this->view->errors = $loginForm->getMessages(); 
     } 
     } 

     $this->view->loginForm = $loginForm; 

形式


class Application_Form_Login extends Zend_Form 
{ 

    public function init() 
    { 
     /* Form Elements & Other Definitions Here ... */ 

    $this ->setName('Login'); 
    $this ->setAction('login') 
     ->setMethod('post'); 

    $name = new App_Form_Element_Text('user_name'); 
    $name ->setLabel('User Name') 
     ->setRequired(true); 
    $pword = new Zend_Form_Element_Password('user_pw'); 
    $pword ->setLabel('Password') 
     ->setRequired(true) 
     ->addValidator('Alnum'); 
    $submit = new Zend_Form_Element_Submit('Submit'); 
    $submit ->setAttrib('id', 'Submit'); 

    $this->addElements(array($name, $pword, $submit)); 

    // buttons do not need labels 
     $submit->setDecorators(array(
      array('ViewHelper'), 
      array('Description'), 
      array('HtmlTag', array('tag' => 'span', 'class'=>'submit-group')), 
     )); 
    } 
} 

这不!

控制器


public function addAction() 
    { 
    $addform = new Application_Form_Student_Add(); 

    //has a form been submitted? 
    if($this->getRequest()->isPost()){ 
     if(isset($_POST['Cancel'])) $this->_redirect('/student'); 
     $formdata = $this->getRequest()->getPost(); 
     if($addform->isValid($formdata)){ 
     Zend_Debug::dump($formdata); 
     } else { 
     $this->view->errors = $addform->getMessages(); 
     } 
    } 
    $this->view->addForm = $addform->generate(); 
    } 

表格


public function init() 
{ 
    $this->studentform = new Zend_Form(); 

    $baseUrl = new Zend_View_Helper_BaseUrl(); 
    $action = $baseUrl->baseUrl() . "/student/add"; 

    $this->studentform->setAction($action); 
    $this->studentform->setName('addStudent'); 
    $this->studentform->setMethod('post'); 

    $student_title = new App_Form_Element_Text('student_title'); 
    $student_title ->setLabel('Titletest') 
     ->setRequired(true); 
    $cancel = new Zend_Form_Element_Submit('Cancel'); 
    $submit = new Zend_Form_Element_Submit('Submit'); 
    $this->studentform->addElement($student_title); 
    $this->studentform->addElement($cancel); 
    $this->studentform->addElement($submit); 
} 

两个正常显示,然而,第一将验证和第二不会。第一个将在提交空白表单时显示错误消息,但第二个总是似乎通过验证,而不管输入的值如何。

我一直在寻找这个小时,它可能只是需要别人来看看代码,并指出了对我来说非常明显。

在这两种情况下,视图只是回声的形式。

回答

3

在工作代码中,您使用$ this在第二个版本中使用$ this-> studentForm在init中引用您的表单。

所以我会很好奇,知道为什么代码不同,那里的第二个init是什么对象。您发布的代码与此不同。

东西告诉我,在你的第二个控制器,你应该使用

$addform->studentForm->stuff 

因为它不是Zend_Form的实例,但包含studentForm Zend_Form的对象。

+0

谢谢你,你已经发现了。我没有注意到我的代码中的差异。正如我所说,我一直在看它太久了! 再次感谢。 – vascowhite 2010-08-29 16:19:09

+0

嘿你的欢迎!请记住在他们帮助和投票时接受答案! – Iznogood 2010-08-29 16:39:22

+1

@vascowhite欢迎来到stackoverflow请在这里阅读常见问题:http://stackoverflow.com/faq – Iznogood 2010-08-29 19:03:22