6

I wish to skip the captcha validation if the account,which maybe a email or mobile, has been captcha validated.

so I write a colsure function. if the session('captcha_validated_reset_account') equals to the account which user input, it will skip the captcha validation.

if not, the program will call a laravel validation rule like "required|captcha"(captcha' comes from https://github.com/mewebstudio/captcha).

The problem is I don't know how to call validation rule in a closure,I tried return 'required|captcha' but it not work as intended.

'captcha' => function($attribute, $val, $fail) use ($request) {
    if (
        ! empty(session('captcha_validated_reset_account'))
        && session('captcha_validated_reset_account') == $request->input('account')
    ) {
        return;
    } else {
        Rule::required... //and call a rule named 'captcha' to validate the captcha code from user input
        //return 'required|captcha';
    }
}

Is any way I can call laravel validation rules in a closure function?

1

1 Answer 1

3

You should put the closure in an array:

'captcha' => [ function ($attribute, $val, $fail) use ($request) {
    if (
        empty(session('captcha_validated_reset_account'))
        && session('captcha_validated_reset_account') != $request->input('account')
    ) {
        $fail($attribute . ' is required.');
    }
}]

Note that I've changed the condition.

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

3 Comments

Thank you for your reply, please notice I wish to call a rule named "captcha" which will validate the captcha code from user input. I don't know how to call this rule in a closure.
oh, sorry I thought that's the name of the filed you are validating. In that case you will need a custom rule, or extend the captcha rule from the library that you are using and override it's passes method to apply your condition.
or check if you can do Rule::captcha(function() {});

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.