Codeigniter:如何使用表单验证正确重定向 [英] Codeigniter: How to redirect properly with form validation

查看:105
本文介绍了Codeigniter:如何使用表单验证正确重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何做一个简单的形式w / o现有值,但我们假设我有一个视图,我可以通过 http://domain.com/account/settings 。让我们说,我有两个字段,用户名,密码和城市,这都是从DB(除了密码当然)。因此,如果用户尝试提交表单,并因任何原因验证失败,我应该在哪里重定向他们?现在,我有它显示相同的视图,但问题是,它从DB拉再次的信息。我应该创建两个不同的视图吗?

I understand how to do it w/ a plain form w/o existing values, but let's say I have a view that I can call via http://domain.com/account/settings. let's say I have two fields, username, password and city, which are all pulled from the DB (except for password of course). So, if a user tries to submit the form and fails validation for whatever reason, where should I "redirect" them to? Right now, I have it showing the same view but the problem is, it pulls the info from the DB again. Should I be creating two different views?

第二个视图本质上显示他们试图输入的信息w /错误消息。

The second view would essentially show the information they tried to enter along w/ the error message.

推荐答案

您不需要两个单独的视图。查看表单助手的功能 set_value() set_select() set_checkbox() set_radio c $ c>。这些在提交和验证后重新填充表单。因此,在您的情况下,您应该以这种方式指定字段:

You do not need two separate views. Check out Form Helper's functions set_value(), set_select(), set_checkbox() and set_radio(). These re-populate form after its submission and validation. So in your case, you should specify the fields this way:

<input type="text"
       name="username"
       value="<?php echo set_value('username', $user['username']); ?>" />
<input type="text"
       name="city"
       value="<?php echo set_value('city', $user['city']); ?>" />

默认情况下,输入将有 $ user ['city'] value。但是验证失败后,将重新填充以前输入的值(包括不正确的值)。

By default, the input will have $user['city'] value. But after failed validation it will be re-populated with previously entered values (including incorrect ones).

请记住,您要重新填充的所有字段都需要传递通过 form_validation 库:

Just remember that all fields you want to re-populate need to be passed through form_validation library:

$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('city', 'City', '');

这篇关于Codeigniter:如何使用表单验证正确重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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