mvc php 中的公式验证

发布于 2024-09-07 13:57:03 字数 1462 浏览 4 评论 0原文

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

我只是不确定如何在 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
}

i have written validation class. now, is it ok to extend a form class from the validation class? or even extending the validation class from the request class?

i'm just not sure how to implement the registration process for a new user in a mvc. totally confuse.

Edit: i have found this zend tut here:

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

but i do not understand how in case of wrong inputs you can go back to the form page now with filled input fields

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

this just sets a value but does not redirect to registration.php. so how do i get to registration.php after this

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
}

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

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

发布评论

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

评论(1

乜一 2024-09-14 13:57:03

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

我建议 如果您想强制验证,请使用依赖注入,或者在必要时选择设置验证对象的选项。我以前都做过。

I wouldn't extend it. They have different "scopes" (one inputs data, and the other validates data)...

I would suggest either Dependency Injection if you want to force validation, or simply the option of setting a validation object if necessary. I've done both before.

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