有关重定向应如何工作的问题 [英] Question about how redirects should work

查看:72
本文介绍了有关重定向应如何工作的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个web应用程序,我正在处理一个表单,要求所有的字段填充,然后提交。如果您尝试提交未填充字段的应用程序,则会再次加载错误页面。一旦你填写了所有的字段,然后点击submit,它会重定向到同一个页面,并显示从flashdata生成的消息。请参阅下面的简化示例。

So I have a web app that i'm working on with a form that requires all of the fields to be populated before submitting. If you try to submit the app without a field being populated, it loads the page again with the errors. Once you fill all the fields in and click submit, it does a redirect to the same page and shows a message which is generated from flashdata. See simplified example below.

欢迎控制器:

function show_view() 
{
  $this->load->view('form');
}

function process_form()
{
  // make the 'quality' field required
  $this->form_validation->set_rules('quality', 'Quality', 'required');

  if($this->form_validation->run() == FALSE) //if the fields are NOT filled in...
  {
    echo form_error('quality');
    $this->load->view('form'); //reload the page  
  }
  else  // if the fields are filled in...
  {
    // set success message in flashdata so it can be called when page is refreshed. 
    $this->session->set_flashdata('message', 'Your rating has been saved');
    redirect(welcome/show_view);
  }
}

现在为了说明我的问题,在'home'视图,我导航到'窗体'视图。如果我填充'质量'字段,并单击提交,我被重定向回到'形式'视图,与成功消息。如果我单击浏览器上的后退按钮,它需要我回到主页视图。一切工作都是预期的

Now to illustrate my issue, lets say I'm on the 'home' view and I navigate to the 'form' view. If i populate the 'quality' field and click submit, i get redirected back to the 'form' view, with a success message. If i click the back button on the browser, it takes me back to the 'home' view. EVERYTHING WORKS AS EXPECTED

现在让我说我在'主页'视图,我导航到'窗体'视图。如果我单击提交按钮,而不填充'质量'字段,'表单'视图重新加载,它显示错误消息。如果我然后填充'质量'字段,并单击提交,我被重定向回到'形式'视图与成功的消息。问题是,如果我单击浏览器上的后退按钮,现在回到表单页面的错误,我必须再次单击返回按钮返回到主页视图。

Now lets say i'm on the 'home' view and I navigate to the 'form' view. If i click the submit button without populating the 'quality' field, the 'form' view is reloaded again and it displays the error message. If i then populate the 'quality' field and click submit, i get redirected back to the 'form' view with a success message. The problem is, if i click the back button on the browser, it now takes me back to the form page with the error, and I have to click the back button again to get back to the 'home' view.

什么是最好的编码实践,所以如果用户提交的表单有错误,它会显示错误,如果他们修复错误,并再次提交表单将显示成功

What is the best coding practice so that if a user submits the form with errors, it will display the errors, and if they fix the errors and submit the form again it will display the success message and if they click back on the browser, it will take them back to the 'home' view??

推荐答案

问题是你使用两个单独的函数来做表单处理。表单验证类docs没有真正解释它,它花了我一段时间来实现它,但form_validation-> run()返回false如果有一个错误,但也是如果它是一个GET请求,并随后考虑在相关函数中的GET请求,如form_error()和validation_errors(),set_value()等。

The problem is that you're using two separate functions to do form handling. The form validation class docs don't really explain it well, and it took me awhile to realize it but the form_validation->run() returns false if there is an error, but also if it is a GET request, and subsequently accounts for the GET request in the related functions like the form_error(), and validation_errors(), set_value(), etc.

CI为此:

class Welcome extends CI_Controller{

function home(){
    $this->load->view('home');
}

function form() 
{
    // make the 'quality' field required
    $this->form_validation->set_rules('quality', 'Quality', 'required');

    // If the fields are NOT filled in...
    // or if there isn't a POST! (check the Form_validation.php lib to confirm)
    if ( $this->form_validation->run() === FALSE) 
    {
         // This form_error() function actually doesn't do anything if there 
         // wasn't a form submission (on a GET request)
         echo form_error('quality');
         $this->load->view('form'); // load or reload the page
    }
    else  // if the fields are filled in...
    {
         // set success message in flashdata so it can be 
         // called when page is redirected. 
         $this->session->set_flashdata('message', 'Your rating has been saved');
         redirect('welcome/home','location', 303);
         exit;
    }

}

action =welcome / form

基本上所有的表单错误函数和所有与表单验证检查表单验证器是否真正运行...这里是form帮助文件中的form_error函数的示例

Basically all of the form error functions and all the stuff related to form validation have checks to see if the form validator actually ran... here is an example from the form_error function in the form helper file

function form_error($field = '', $prefix = '', $suffix = '')
{
    if (FALSE === ($OBJ =& _get_validation_object()))
    {
        return '';
    }

    return $OBJ->error($field, $prefix, $suffix);
}

当它们不是POST时,它显示为正常,您正在寻找的自然页面流。

When their isn't a POST, it shows as normal, and has the natural page flow you are looking for.

与问题无关,但混淆/值得注意的表单验证类...如果您在参数字段中使用像xss_clean,prep_url等过滤器,实际上为你重新填充$ _POST数组,所以你真的不需要做任何额外的工作。

Unrelated to the question, but confusing/noteworthy about the form validation class... if you use the filters like xss_clean, prep_url, etc. in the parameters field, it actually repopulates the $_POST array for you, so you don't really need to do anything extra.

有时值得看看CI源的内部,有一些聪明的东西在那里是不完全明显的。

Sometimes it's worth taking a look at the internals of the CI source, there's some clever stuff in there which isn't entirely obvious.

这篇关于有关重定向应如何工作的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆