Welcome Guest, Not a member yet? Register   Sign In
Simple Form Validation Mod: Check if error exists
#1

[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&lt;?php echo ($this-&gt;form_validation-&gt;error_exists('email') ? ' error' : ''); ?&gt;">
<label>Field</label>
&lt;input type="email" name="email" id="email" /&gt;
&lt;?php echo form_error('email'); ?&gt;
</div>

Hope this helps others, though by now they've probably rolled their own solution for this. Smile




Theme © iAndrew 2016 - Forum software by © MyBB