Zend Form 不验证输入

发布于 2024-09-16 20:18:35 字数 3241 浏览 3 评论 0原文

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);
 }

两者均显示正常,但第一个将验证,第二个则不会。当提交空白表单时,第一个将显示错误消息,但是无论输入的值如何,第二个似乎总是通过验证。

我已经研究这个问题几个小时了,它可能只需要其他人来查看代码并指出对我来说非常明显的内容。

在这两种情况下,视图只是呼应表单。

Zend Form is not my friend today!

This works :-

Controller:-


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;

Form


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')),
        ));
    }
}

This doesn't!

Controller


 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();
    }

Form


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);
 }

Both display properly, however the first will validate and the second won't. The first will display error messages when a blank form is submitted, however the 2nd always seems to pass validation regardless of the values entered.

I've been looking at this for hours and it just probably needs somebody else to look at the code and point out the blindingly obvious to me.

In both cases the view just echos the form out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

作业与我同在 2024-09-23 20:18:35

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

所以我很想知道为什么代码不同以及第二个 init 来自哪个对象。您发布的代码在那里有所不同。

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

$addform->studentForm->stuff

因为它不是 Zend_Form 的实例,而是包含 StudentForm Zend_Form 的对象。

In the working code you use $this to refer to your form in init in the second version you use $this->studentForm.

So I would be curious to know why the codes differ there and what object is that second init from. The code you posted is different there.

Something tells me in your second controller you should be using

$addform->studentForm->stuff

Since its not an instance of Zend_Form but an object containing a studentForm Zend_Form.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文