如何在codeigniter中使用重定向显示验证错误? [英] How to show validation errors using redirect in codeigniter?

查看:130
本文介绍了如何在codeigniter中使用重定向显示验证错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经处理了一段时间的问题。如何在函数中使用重定向设置验证错误?这是我在控制器中的代码:

I have been dealing with a problem for a while. How can I set the validation errors using redirect in a function? This is the code I have in my controller :

function send()
{
    $this->form_validation->set_rules('e-mail', 'Email', 'trim|required|valid_email');
    $this->form_validation->set_rules('cellphone', 'Cellphone Number', 'trim|required|is_natural');
    $this->form_validation->set_message('required', '%s is required.');
    $this->form_validation->set_message('valid_email', '%s is not a valid Email Address');
    $this->form_validation->set_message('is_natural', '%s can only contain numbers.');
    $this->form_validation->set_error_delimiters('<li>', '</li>');

    if($this->form_validation->run() == FALSE)
    {
        redirect ('/');
    }
    else
    {
        echo '<pre>';
        print_r($_POST);
        echo '<pre>';
    }
}

这是我在我的视图文件中使用的代码:

And this is the code I use in my view file:

<? if (validation_errors())
{   
echo '<div id="validation_errors" title="Error:">';
echo '<div class="response-msgs errors ui-corner-all"><span>Errors:</span><br /><ul>';
echo validation_errors();
echo '</ul></div>';
echo '</div>';
}
?>


推荐答案

我找到了办法。重定向不会使数据保持显示。我使用下面的代码来解决问题:

I found the way to do it. Redirecting does not keep the data to be shown. I used the code below to solve the problem:

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

这篇关于如何在codeigniter中使用重定向显示验证错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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