[eluser]Kinsbane[/eluser]
So, currently my form is setup to where labels and inputs are housed within a container. My CSS is setup to show a light red background, a red border on the input, and red text if the container div has the class "error". This is well and good, but I realized that while using CI's Form Validation, I have no way of checking if the error of a field is actually present, in order for me to be able to assign the "error" class to my containing div.
I have searched around for this, but didn't have much luck in finding a similar solution.
So, this is my simple fix. It's more than likely not best practice, but this is how I thought to do it.
Code:
class MY_Form_validation extends CI_Form_validation
{
/**
* Check if provided field has an error in $this->_field_data
*
* @access public
* @param string
* @return bool
*/
function error_exists($field)
{
if(!empty($this->_field_data[$field]['error']))
{
return TRUE;
}
return FALSE;
}
}
Then, in my view:
Code:
<div class="container<?php echo ($this->form_validation->error_exists('email') ? ' error' : ''); ?>">
<label>Field</label>
<input type="email" name="email" id="email" />
<?php echo form_error('email'); ?>
</div>
Hope this helps others, though by now they've probably rolled their own solution for this.