CodeIgniter突出显示表单提交错误的字段 [英] CodeIgniter highlight fields with errors on form submission

查看:53
本文介绍了CodeIgniter突出显示表单提交错误的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用一些指导来指导如何在CodeIgniter中进行表单验证。我正在使用CodeIgniter的内置表单验证功能,并且可以正常运行。通过使用错误并将其包装在某些HTML / CSS中以进行样式设置,它将为每个出现错误的字段返回单独的错误消息:

I could use some guidance in how to proceed on form validation in CodeIgniter. I am using CodeIgniter's built-in form validation and works fine as far as it goes. It returns individual error messages for each field where there is an error by using and wrapping it in some HTML/CSS for styling:

<?php echo validation_errors('<p class="error">'); ?>

但是我们要做的是突出显示有错误的字段。 CI将允许您将错误消息放在表单错误所在的位置旁边。但这要求您使用错误消息作为值,例如:

But what we want to do is highlight the fields where there are errors. CI will let you put the error messages next to where the form errors are. But it requires you to use the error message for the value, like this:

<input type="text" name="email" value="<?php echo set_value('email'); ?>" size="50" />

顺便说一句,这是CI手册中非CI语法的内容,这令我感到困惑。无论如何,我们还希望保留提交表单时来自字段的数据并返回。所以我做了:

which by the way is in CI's manual in non-CI syntax, which puzzles me. Anyway, we also want the data from the field when the form is submitted to be preserved and returns. So I've done:

$email = array('name' => 'email', 'value' => $em);
      ?><div style="padding:5px;">Email* </div><?php echo form_input($email) . '<br/>';

$ em是从控制器返回的,如下所示:

$em is returned from the controller like this:

$data['em'] = $this->input->post('email');

所以我的问题是,我该如何完成以上概述的所有工作?显然,CI的建议与我所做的相冲突。但是我不知道该怎么做,所以我可以使用一些帮助。

So my question is, how do I accomplish all of what is outlined above? Obviously, what CI suggests and what I have done collide. But I don't know how else to do it, so I could use some help.

编辑:进一步挖掘后,我发现可以将错误消息放在下一个

Upon further digging, I see that you can put the error message next to the field by doing this:

<?php echo form_error('email'); ?>

但是我没有收到任何关于错误的消息,即使我已经写了规则并且

But I'm not getting any message upon an error, even though I have the rule written and I get an error with the first line of code above.

推荐答案

为了单独显示错误,应使用form_error('电子邮件')。为了让您获得要检查的字段的值,请使用函数set_value('email')。为了使这两个功能正常工作,您必须在控制器中为电子邮件字段设置一个规则。

In order to display error individually you should use the function form_error('email'). And for you to get a value of a field being checked, use the function set_value('email'). For these two functions to work, you would have had to, in your controller, set a rule for the 'email' field. Where you specify wich validation rules apply to that field.

<?php echo form_error('email'); ?>
<input type="text" name="username" value="<?php echo set_value('email'); ?>" size="50" />

来源: http://codeigniter.com/user_guide/libraries/form_validation.html#individualerrors

这篇关于CodeIgniter突出显示表单提交错误的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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