mvc php 中的公式验证
我已经写了验证类。现在,可以从验证类扩展表单类吗?或者甚至从请求类扩展验证类?
我只是不确定如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不会延长它。它们有不同的“范围”(一个输入数据,另一个验证数据)...
我建议 如果您想强制验证,请使用依赖注入,或者在必要时选择设置验证对象的选项。我以前都做过。
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.