0

I am using Codeigniter 3.x form validation callback method in combination trim and required to validate a field. The problem is, when I pipe them: trim|required|callback_some_method, the callback method seems to take precedence over trim and required and shows its error message. Any ideas on this?

EDIT: This is the rule:

$this->form_validation->set_rules('new_password', 'New Password', 'trim|required|min_length[8]|callback_password_check');

And this is the password_check method:

function password_check($pwd) {
    $containsLetterUC = preg_match('/[A-Z]/', $pwd);
    $containsLetterLC = preg_match('/[a-z]/', $pwd);
    $containsDigit = preg_match('/\d/', $pwd);
    $containsSpecial = preg_match('/[^a-zA-Z\d]/', $pwd);

    if ( !($containsLetterUC && $containsLetterLC && $containsDigit && $containsSpecial) ) {
        $this->form_validation->set_message('password_check', '{field} must contain UPPERCASE and lowercase letters, digits, and special characters.');
        return FALSE;
    }       

    return TRUE;
}

The method should return FALSE, but as long as required is before my custom rule and the field is empty, it should stop there with Required field message, NOT the custom method message.

1
  • That would tend to indicate that some_method is returning false. trim will never return false and if required returns false some_method won't be run. It's hard to offer advice without seeing code for some_method. Commented Feb 8, 2017 at 23:15

2 Answers 2

2

Okay guys, I've managed to solve it by extending the Form_validation library, putting my callback method there and piping as the other rules (without callback_ prefix).

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

Comments

0

Unfortunately, as described in the code from CI, callbacks validation rules are always verified first, prior to ‘required’ for instance.

There is an official issue opened at CI : https://github.com/bcit-ci/CodeIgniter/issues/5077

Comments

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.