Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dan1234827's avatar

Returning data from a custom validator

I have a custom validator rule, which is validating a ReCAPTCHA v3 token and it's working fine but I was wondering if there's any way to return the score? (I really just need it for logging purposes. the pass/fail decision is made by the rule)

0 likes
2 replies
LaryAI's avatar
Level 58

In Laravel, when you create a custom validation rule, you typically return true or false to indicate whether the validation passed or failed. However, if you want to return additional data such as the ReCAPTCHA score for logging purposes, you can't directly return it from the validation rule itself.

Instead, you can use the Laravel's Validator facade to hook into the validation process and store the additional data you need. Here's how you can do it:

  1. Create a custom validation rule for ReCAPTCHA.
  2. Store the score in the session or pass it through an event.
  3. Log the score after the validation is completed.

Here's an example of how you might implement this:

// Create a custom validation rule
use Illuminate\Contracts\Validation\Rule;

class Recaptcha implements Rule
{
    public $score;

    public function passes($attribute, $value)
    {
        // Perform ReCAPTCHA validation and store the score
        $recaptchaResponse = $this->validateRecaptcha($value);
        $this->score = $recaptchaResponse->score;

        // You might want to store the score in the session or dispatch an event here
        session(['recaptcha_score' => $this->score]);

        // Return true or false based on the validation
        return $recaptchaResponse->success;
    }

    public function message()
    {
        return 'The ReCAPTCHA verification failed.';
    }

    protected function validateRecaptcha($value)
    {
        // Your ReCAPTCHA validation logic here
        // This should return an object or array with 'success' and 'score' properties
    }
}

Then, when you use this rule in a form request, you can retrieve the score from the session or listen for the event you dispatched and log the score:

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

public function store(Request $request)
{
    $validator = Validator::make($request->all(), [
        'g-recaptcha-response' => ['required', new Recaptcha],
    ]);

    if ($validator->fails()) {
        // Handle the failed validation
    } else {
        // The validation passed, you can log the score now
        $score = session('recaptcha_score', null);
        // Log the score
        \Log::info("ReCAPTCHA score: {$score}");

        // Continue with your controller logic
    }
}

Remember to clear the session after logging the score to avoid old scores being logged on subsequent requests.

This approach allows you to perform the validation and also use the score for logging or any other purposes you might have.

KalimeroMK's avatar
public function passes($attribute, $value)
{
    // Your existing ReCAPTCHA validation logic
    $response = // ... make HTTP request to verify ReCAPTCHA token ...

    if ($response->isSuccess()) {
        $score = $response->getScore();
        // Store the score in session or log
        session(['recaptcha_score' => $score]);
        // or
        Log::info('ReCAPTCHA score: ' . $score);

        return true;
    }

    return false;
}

Please or to participate in this conversation.