I am learning codeigniter and I have two questions about form validation. I have a controller, that uses an array of configuration for the form validation. Something like this:
public $validacao = array(
array(
'field' => 'nome',
'label' => 'Nome',
'rules' => 'required',
"errors" => array('required' => " Informe o %s. ")
),
array(
'field' => 'descricao',
'label' => 'Descrição',
'rules' => 'required',
"errors" => array('required' => " Informe a %s. ")
));
I have a custom library, named IncFuncoes.php, inside application/libraries. It only has a function that validates date. I wanna call this function as a callback on the 'rules' of my array, but it doesnt work. After I loaded it inside the constructor, I did something like this:
public $validacao = array(
array(
'field' => 'nome',
'label' => 'Nome',
'rules' => 'required',
"errors" => array('required' => " Informe o %s. ")
),
array(
'field' => 'descricao',
'label' => 'Descrição',
'rules' => 'required',
"errors" => array('required' => " Informe a %s. ")
)
array(
'field' => 'data',
'label' => 'Data',
'rules' => 'required|$this->incfuncoes->validatedate',
"errors" => array('required' => " Informe a %s. ")
));
But it didnt work. If I create a function inside my controller that only calls the function inside my custom library, it works. But I was wondering if there is any way that I could call the function inside my library directly on the 'rules' of my configuration array.
My other question is, how do I customize an error message using this configuration array for my form validation. Cause the way that I am doing is for each validation, like required for instance, I create an error message. I wanna create one error for each input. Like, if the field is empty or the date format is not right, I only need the message "Wrong date format.". I know that could use something like this:
$this->form_validation->set_message('name', 'required', 'Enter your Name');
But I wanna use only my configuration array to set everything. Is it possible?