php - Use a callback function on form validation rules from an external library - Codeigniter

984

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?

42

Answer

Solution:

What you should do is extend the form validation library itself.

class MY_Form_validation extends CI_Form_validation
{
    public function validatedate($input)
    {
        // set error message
        // do your validation
        // make sure to return true or false
    }
}

Then apply via:

'rules' => 'required|validatedate',

People are also looking for solutions to the problem: php - How to retrieve info from SimpleXMLElement object

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.