【问题标题】:formular validation in mvc phpmvc php中的公式验证
【发布时间】:2010-07-02 09:25:57
【问题描述】:

我已经编写了验证类。现在,可以从验证类扩展一个表单类吗?甚至从请求类扩展验证类?

我只是不确定如何在 mvc 中为新用户实施注册过程。完全糊涂了。

编辑:我在这里找到了这个zend tut:

// application/controllers/GuestbookController.php
  class GuestbookController extends Zend_Controller_Action

  {
      // snipping indexAction()...

      public function signAction()
      {
          $request = $this->getRequest();
          $form    = new Application_Form_Guestbook();

          if ($this->getRequest()->isPost()) {
              if ($form->isValid($request->getPost())) {
                  $comment = new Application_Model_Guestbook($form->getValues());
                  $mapper  = new Application_Model_GuestbookMapper();
                  $mapper->save($comment);
                  return $this->_helper->redirector('index');
              }
          }

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

但我不明白如果输入错误,您现在如何返回填写输入字段的表单页面

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

这只是设置一个值,但不会重定向到registration.php。那么在此之后我如何访问registration.php

if ($form->isValid($request->getPost())) {
    $comment = new Application_Model_Guestbook($form->getValues());
    $mapper  = new Application_Model_GuestbookMapper();
    $mapper->save($comment);
    return $this->_helper->redirector('index');
}
else {
    // ... do redirect to registration.php and fill input fields with set $_POST
}

【问题讨论】:

    标签: php model-view-controller validation forms registration


    【解决方案1】:

    我不会扩展它。它们有不同的“范围”(一个输入数据,另一个验证数据)...

    如果您想强制验证,我建议Dependency Injection,或者在必要时简单地设置验证对象。我以前都做过。

    【讨论】:

      猜你喜欢
      • 2012-11-19
      • 2010-12-26
      • 2012-01-20
      • 2015-02-06
      • 2012-07-31
      • 1970-01-01
      • 1970-01-01
      • 2011-08-13
      • 2015-08-06
      相关资源
      最近更新 更多