如何通过redirect()传递验证错误数据? [英] How to pass validation error data through redirect()?

查看:105
本文介绍了如何通过redirect()传递验证错误数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含表单的页面,当任何用户提交它时,数据会传送到控制器,控制器会检查验证,如果有任何错误,它会将用户重定向到上一页(包含形式),否则将数据发送到模型。

I have a page that contains a form and when any user submits it, the data goes to a controller and the controller checks the validation, if there is any error it redirects the user to the previous page( the page that contains the form), otherwise it sends data to models.

要从控制器重定向到上一页(如果有任何验证错误),我有以下代码

To redirect to the previous page from controller (if there is any validation error), I have the following code

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

上面的代码工作正常,但问题是它将用户重定向到上一页页面包含的形式)它不显示验证错误。

The above code is working fine but the problem is after it redirects the user to the previous page(The page that contains the form) it is not displaying the validation errors.

所以,请你能告诉我如何通过这个重定向代码传递验证错误信息我

So would you please kindly tell me how to pass the validation error information through this "redirect" code I posted above and show the validation error message on that page?

感谢:)

在我的控制器中:

   $redirect=$this->input->post('redirect'); //  << for this I have- <input name="redirect" type="hidden" value="<?= $this->uri->uri_string() ?>" />         in my view file

   $this->session->set_flashdata('errors', validation_errors());
   redirect($this->input->post('redirect')); 

在我的视图文件中:

   <?php

    if ($this->session->flashdata('errors')){ //change!
    echo "<div class='error'>";
    echo $this->session->flashdata('errors');
    echo "</div>";
    }

    ?>

因此,我可以通过从控制器到上一页的重定向传递验证错误数据

Thus I can pass validation error data through redirect from controller to previous page

推荐答案

您可以从CI的会话库尝试flashdata。

You can try flashdata from CI's session library. It makes the data available for the next server request.

$this->session->set_flashdata('errors', validation_errors());

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

这篇关于如何通过redirect()传递验证错误数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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