【问题标题】:Codeigniter Form Validation: How to redirect to the previous page if found any validation error?Codeigniter 表单验证:如果发现任何验证错误,如何重定向到上一页?
【发布时间】:2011-10-28 18:26:48
【问题描述】:

我正在使用 Codeigniter 的验证类来验证我的表单。如果发现任何验证错误,请告诉我如何从控制器重定向到上一页?

在我的控制器中:

if ($this->form_validation->run() == FALSE){

  //**** Here is where I need to redirect  

} else  {
     // code to send data to model...           

  }                             

【问题讨论】:

    标签: codeigniter codeigniter-2


    【解决方案1】:

    我为此扩展了 URL 助手。

    https://github.com/jonathanazulay/CodeIgniter-extensions/blob/master/MY_url_helper.php

    在您的控制器中:

    $this->load->helper('url');
    redirect_back();
    

    只需将MY_url_helper.php 放入application/helpers 即可。

    【讨论】:

    • 一点反馈:这个 URL 助手非常适合我。非常感谢。在我看来,最好为这样的事情做助手。
    • @AlexSpencer 谢谢!只是想在这里说明一下,http 在第 17 行是硬编码的(不是一个大问题,但仍然如此)。这应该是固定的,它对拉取请求开放。
    【解决方案2】:

    更新

    您想发布一个表单,对其进行验证,然后在验证失败时再次显示表单并显示验证错误,或者如果验证通过则显示完全不同的内容。

    执行此操作的最佳方法是将表单发回给自身。所以你的表单的动作是action=""。这样,在您的方法中,您可以检查表单是否已提交,并确定在那里做什么:

    // in my form method
    if ($this->input->post('submit')) // make sure your submit button has a value of submit
    {
         // the form was submitted, so validate it
         if ($this->form_validation->run() == FALSE)
         {
             $this->load->view('myform');
     }
     else
     {
             $this->load->view('formsuccess');
     }
    }
    else
    {
        // the form wasn't submitted, so we need to see the form
        $this->load->view('myform');
    }
    

    老答案

    您始终可以在表单的隐藏字段中传递当前 URI:

    <input name="redirect" type="hidden" value="<?= $this->uri->uri_string() ?>" />
    

    如果验证失败则重定向:

    redirect($this->input->post('redirect'));
    

    或者您可以在 flashdata 会话变量中设置重定向 url:

    // in the method that displays the form
    $this->session->set_flashdata('redirect', $this->uri->uri_string());
    

    如果验证失败则重定向:

    redirect($this->session->flashdata('redirect'));
    

    【讨论】:

    • 感谢您的回复。您发布的前两个代码完全可以用于显示验证错误。所以我试图使用你发布的后两个代码,但由于我对 PHP/codeigniter 知之甚少,我不确定在哪里使用这些代码。我应该粘贴这个“$this->session->set_flashdata('redirect', $this->uri->uri_string());”在我上面发布的控制器上还是以我提到的形式?再次感谢:) 对不起我的愚蠢问题。 :(
    • 无论哪种方式,除非您也通过了验证错误,否则您不会收到验证错误。我将更新我的答案以反映您需要显示验证错误。
    【解决方案3】:

    嗯,通常你应该这样做(现在是伪代码):

    • 如果 form_validation == false --> 表单尚未提交或验证失败 --> 加载表单视图;
    • 如果 form_validation == true --> 进行处理。

    这意味着您必须留在同一个控制器中。您的代码应该已经在执行此行为,这是预期的行为。

    如果您仍然有重定向的冲动,请调用相应的函数:

    redirect('updatebatch/get/40','refresh');
    // assuming 'updatebatch' is the name of your controller, and 'sundial' just a folder
    

    【讨论】:

    • 感谢您的回复。抱歉,我应该早一点说明 - 在localhost/sundial/updatebatch/get/40- 中,“40”的部分是可变的。它可能会不时改变。在那种情况下我该怎么办?为了清楚起见,这里的 40 是表中特定行的 id 号。我只是使用 id 来获取数据。非常感谢您的帮助:)
    【解决方案4】:

    我在库中创建了一个函数,以便在需要时创建重定向。

    <?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
    
    class Functions {
    
    public function generateRedirectURL()
    {
    $CI =& get_instance();
    $preURL = parse_url($_SERVER['REQUEST_URI']);
    $redirectUrl = array('redirectUrl' => 'http://' . $_SERVER['SERVER_NAME'] . $preURL['path']);
    $CI->session->set_userdata($redirectUrl);
    }
    
    }
    
    //End of the file
    

    当你想创建到该页面的重定向时,只需在函数上写:

    $this->load->library('functions'); //you can put it in the autoloader config
    $this->functions->generateRedirectURL();
    

    那么你只需要调用:

    redirect($this->session->userdata['redirectUrl']);
    

    【讨论】:

      猜你喜欢
      • 2013-03-24
      • 2019-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多