CodeIgniter Forums

Full Version: Validation callback
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I have problem.
I get only callback validation.
Why?

PHP Code:
$this->form_validation->set_rules(
            'user_passcurr',
            'current password',
            'trim|required|min_length[6]|max_length[32]|callback_check_passcurr|strip_tags',
            [
            'required' => 'error0',
            'min_length' => 'error1',
            'max_length' => 'error2',
            ]
        ); 

PHP Code:
    public function check_passcurr(string $str)
    {
        if ($this->user_model->passcurr_verify($this->session->user_id$str)) {
            return TRUE;
        } else {
            $this->form_validation->set_message('check_passcurr''Error {field}');
            return FALSE;
        }
    
I think what you mean is that even though you would expect the required, min length and max length to apply to an incorrect input and fail on that, instead you are getting the error message for the callback, not the previous ones.

I could be wrong here, but I recall something along the lines of that callbacks get applied first. So your callback is being run first, it fails validation, and the testing stops there.

I am surprised your callback works though because you normally have to prefix it as 'callback_check_passcurr' so the validation library knows it is a callback. The function is still called check_passcurr. https://www.codeigniter.com/user_guide/l...on-methods

Sometimes, when I cannot do a callback because I need the other things validated first, I would do the validation check (required min_length etc) as per normal, and once passed validation, I do the further checks I need to do like check_passcurr, and output an error message separate from the form validation.

In my head this makes sense, as the form validation is checking the data received is in the format I expected, the further checks like is the password current, or is the code matched to a discount structure or whatever, has nothing to do with if the data is of the right form or not. It is almost as if we are trying to do too much business logic within a simple form validation. I expect 8 chars, I get 8 chars, validation passed. Whether those eight chars are matched to anything, or if they serve a particular business logic requirement or not, is not really anything to do with form input validation. I know people will have different opinions on this, just my humble opinion.

Hope that helps,

Paul

PS Just to clarify what I mean, a form submission fails when the data is not of the correct form. So a required field of max_length 20 must be there and must be less than 20 chars. However, what happens with correct data is subject to my app and the business logic. A correctly formatted but wrong code for instance might disable an account, might reset an email validation process, might trigger other alerts, might adjust prices for products etc etc. But the form does not fail in the submission, the user has filled in the form correctly. What the user sees next is up to me, they might see the form again and a message, but they might be redirected to an error page, a homepage with settings changed, logged out, banned etc etc. Too much business logic to include in a simple form validation.
To invoke a callback just put the method name in a rule, with “callback_” as the rule prefix.
If you need to receive an extra parameter in your callback method, just add it normally after
the method name between square brackets, as in: callback_foo[bar], then it will be passed as
the second argument of your callback method.
Sorry, I mean "callback_check_passcurr" ... this example with error, I was typing in dev with callback prefix
(11-19-2017, 07:08 AM)PaulD Wrote: [ -> ]In my head this makes sense, as the form validation is checking the data received is in the format I expected, the further checks like is the password current, or is the code matched to a discount structure or whatever, has nothing to do with if the data is of the right form or not. It is almost as if we are trying to do too much business logic within a simple form validation. I expect 8 chars, I get 8 chars, validation passed. Whether those eight chars are matched to anything, or if they serve a particular business logic requirement or not, is not really anything to do with form input validation. I know people will have different opinions on this, just my humble opinion.

I made this for less request for mysql (check password). If you typing small password I can't request to database for in vain.

I still have problem.
I want get callback with other validations, or this impossible?
(11-20-2017, 08:02 AM)rolly Wrote: [ -> ]I made this for less request for mysql (check password). If you typing small password I can't request to database for in vain.
Then min_length would work here surely? Assuming you are not doing your business logic in your form validation. After all 'dsgsdgdsg' is a valid format for a password, even though it might not be the correct password.

(11-20-2017, 08:02 AM)rolly Wrote: [ -> ]I still have problem.
I want get callback with other validations, or this impossible?
Callbacks are fine. You can use as many as you wish. The only point I was making was that I believe the callbacks are run first, hence answering your original question.

Personally, I do not do things like 'is this the correct password' in form validation, only that a password of the right format has been submitted. Other things I see not as form validation issues, but business logic issues.

Anyway, I hope that helps,

Paul
ok, I'm understand. Callback call first. Thank you all.