English 中文(简体)
CodeIgniter form verification and class
原标题:

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

<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?





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签