开发者

CodeIgniter form verification and class

开发者 https://www.devze.com 2022-12-10 11:11 出处:网络
I\'m using the form validation library and have something like this in the view &开发者_运维问答lt;p>

I'm using the form validation library and have something like this in the view

&开发者_运维问答lt;p>
        <label for="NAME">Name <span class="required">*</span></label>
        <?php echo form_error('NAME'); ?>
        <br /><input id="NAME" type="text" name="NAME" class="" value="<?php echo set_value('NAME'); ?>"  />
</p>

I'd like to add a class to the input that is dependent on the form error so i could have something like this

<input id="NAME" type="text" name="NAME" class="<?php echo $error;?>" value="<?php echo set_value('NAME'); ?>"

I realize that it would have to be a bit more complicated, but I'd like to this this without creating a custom rule callback for every field.

Thanks ~Daniel


If you set all your validation fields in your controller using:

$this->validation->set_fields()

You will have access to any possible error messages, which you could use in the following manner:

<input id="NAME" name="NAME" value="<?php echo $this->validation->NAME; ?>" <?php echo (!empty($this->validation->NAME_error)) ? 'class="error"' : ''; ?> />

http://codeigniter.com/user_guide/libraries/validation.html

As pointed out in the comments, the code above references the old validation library. The new 1.7 way to perform this task would be:

<input id="NAME" name="NAME" value="<?php echo set_value('NAME'); ?>" <?php echo (!empty(form_error('NAME')) ? 'class="error"' : ''; ?> />


I just realized, there is a way to do this with error delimiters

$this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');

I this makes it much easier.

But just out of curiosity, Is there any way of doing this with class in the input?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号