0

I'm working with Codeigniter 2.2.5 and PHP 5.6

I want to validate the user input with the codeiginter functions. This is the normal way:

$this->form_validation->set_rules('mail', 'E-Mail', 'required|trim|xss_clean|strip_tags');

Here I would like to add a function that checks whether the e-mail address is contained in a defined list.

There is a callback function for custom rules, but this only works in the controller class. I'm in a third party library class.

Like

$this->form_validation->set_rules('username', 'Username', 'callback_username_check');

Does anybody have an idea on how to achieve this with CodeIgniter 2 or do I have to check it on my own and create a custom error message?

I know that in CodeIgniter 3 there are new functionalities, but unfortunately, I have to work with CodeIgniter 2.

2
  • then just make that rule where you're creating the rule Commented Jul 12, 2019 at 8:29
  • custom rules only work if you are inside a controller Commented Jul 12, 2019 at 10:01

2 Answers 2

1

In codeigniter 2 it's not possible to use a callback method from outside a controller. So if you are in a library or in a model, you have to do a workaround.

What you should do is have a helper function that you load prior to initialization of the model/library you are in. Then you treat it as a normal PHP function and not a 'callback'.

Like this:

$this->form_validation->set_rules('username', 'Username', 'username_check');

Where username_check is the function you have loaded in the helper.

Anyways, there is a difference between data manipulation and data validation. Validation only should happen in a controller. So you may have to refactor your code. This will help you in the long run.

Sign up to request clarification or add additional context in comments.

1 Comment

I've added an answer, in the comment section arent enough characters.
0

@Pedro Martins

Thank you. I created a helper class and implemented the check function. The function is called correctly and the value to be checked is pass trough it. In my check function I return TRUE if the value is ok, otherwise FALSE.

LibraryCode:

$this->form_validation->set_rules('mail', 'E-Mail', 'checkMail');

Helper Class:

function checkMail($value)
{
    if($value == ".....") //if clause works correctly
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

However, the form-validation function always returns TRUE, no matter what my custom helper-check function returns.

$this->form_validation->set_rules('mail', 'E-Mail', 'checkMail');       

    //if form validation false -> show form again
    if ($this->form_validation->run() == FALSE)
    {
        // ...... is never triggered
    }
    else //if form validation success, proceed
    {
        // ......
    }

The program structure is unfortunately so given, therefore I can not locate my code in the controller.

1 Comment

Refactor your code, so that only in CONTROLLERS you do data validation. The workaround I talked about is not good long term.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.