dsfdfdfd6576578 2011-03-01 16:35
浏览 66
已采纳

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.

  • 写回答

3条回答 默认 最新

  • du94414 2011-03-01 16:59
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助