CodeIgniter突出显示表单提交时出错的字段
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">'); ?>
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" />
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 is returned from the controller like this:
$data['em'] = $this->input->post('email');
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.
EDIT: 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.
我可以使用一些指导来了解如何在CodeIgniter中进行表单验证。 我正在使用CodeIgniter的内置表单验证,并且可以正常工作。 它通过使用并将其包装在某些HTML / CSS中进行样式化,为每个出错的字段返回单独的错误消息: p>
&lt;?php echo validation_errors('&lt; p class =“error”&gt;'); ?&gt;
code> pre>
但我们要做的是突出显示有错误的字段。 CI将允许您将错误消息放在表单错误所在的位置旁边。 但它要求您使用错误消息作为值,如下所示: p>
&lt; input type =“text”name =“email”value =“&lt;?php echo set_value('email');?&gt;“ size =“50”/&gt;
code> pre>
顺便说一句,在非CI语法的CI手册中,这让我很困惑。 无论如何,我们还希望在提交表单时保留并返回来自字段的数据。 所以我已经完成了: p>
$ email = array('name'=&gt;'email','value'=&gt; $ em);
?&gt; &lt; div style =“padding:5px;”&gt;电子邮件*&lt; / div&gt;&lt;?php echo form_input($ email)。 '&lt; br /&gt;';
code> pre>
$ em从控制器返回如下: p>
$ data ['em'] = $ this-&gt; input-&gt; post('email');
code> pre>
所以我的问题是,我该如何完成 所有上面列出的内容? 显然,CI建议和我所做的事情相互冲突。 但是我不知道怎么做,所以我可以使用一些帮助。 p>
编辑:进一步挖掘后,我看到你可以把错误信息放在字段旁边 这样做: p>
&lt;?php echo form_error('email'); ?&gt;
code> pre>
但即使我写了规则并且第一行代码出错,我也没有收到任何错误消息 上面。 p>
div>
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" />
source: http://codeigniter.com/user_guide/libraries/form_validation.html#individualerrors
untested, but form_error($field)
should return true if there is an error:
So perhaps:
<input type="text" name="email" <?php if (form_error($email)) { echo 'class="error"'; } ?> value="<?php echo set_value('email'); ?>" size="50" />
untested. worth a shot?
Perhaps also consider using JQuery or similiar to validate and style the form fields, and use CI as a fallback (for presentation purposes, obviously).
That way your form validation can be styled as you required without CI limitations for 99% of validation rules, and then anything else, the default CI way can kick in.
form error($field) returns an empty string '' so better use:
<input type="text" name="email" <?php if (form_error($email) !=='') { echo 'class="error"'; } ?> value="<?php echo set_value('email'); ?>" size="50" />
Tested.