[eluser]Exangelus[/eluser]
Hi all,
I'm currently building an framework within CI to easily maintain all sorts of forms.
For this purpose we're making a library that utilizes the excellent FormGen library from Frank Michel and the Livevalidation js class from Alec hill.
Now I'd like to extend CI's Form_validation class because we like to set custom error
messages for each field. Therefore I tend to extend the _execute function and add some functinality to replace set error messages for custom one's.
Here's the code: (I'll add a simple function to set the custom messages)
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* MY_Form_validation is used to add field-custom error messages to CI's Form_validation
*
* @param array contains custom error messages for specific fields. key => field, value => the error message
*/
class MY_Form_validation extends CI_Form_validation
{
private $custom_error_messages = array();
/**
* Extend the parent's execute to check whether their are custom error messages stored, remove any old errors from this
* field and than add the custom error
*
* @access private
* @param array
* @param array
* @param mixed
* @param integer
* @return mixed
*/
public function _execute($row, $rules, $postdata = NULL, $cycles = 0)
{
//first have pappy do all the work
parent::_execute($row, $rules, $postdata = NULL, $cycles = 0);
//loop through all fields
foreach($this->_field_data as $field => $data)
{
//check whether their was an error set
if(isset($this->_field_data[$field]['error']))
{
//and if their is a custom error available, put it in
if(isset($this->custom_error_messages[$field]))
{
$this->_field_data[$field]['error'] = $this->custom_error_messages[$field];
$this->_error_array[$field] = $this->custom_error_messages[$field];
}
}
}
}
}
/* End of file MY_Form_validation.php */
/* Location: ./application/libraries/MY_Form_validation.php */
We willfully cancel the option for multiple error's for one field for our project's purpose
Now something is going wrong, the form always validates false. I'm not quit sure how the return value of _execute influences this but I have tried most logical options (true, false, empty array, nothing) but it always validates false..
Thanks in advance!