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

insight's avatar

How to show google recaptcha validation Error ?

Dear Friends, I am using google recaptcha 2 for avoiding spam form submission. I am using an ajax function to validate form. It's controller is

public function ServerValidation(Request $request)
    {
       
      
        $validator = Validator::make($request->all(), [
            'inputName' => 'required',
            'inputPost' => 'required|integer',
            'identification_document_type' => 'required',
            'email' => 'required|max:255',
            'inputPermanent' => 'required',
            'inputCommunication' => 'required',
            'inputState' => 'required|string',
            'inputDistrict' => 'required|string',
            'degree_name.*' => 'required',
            'universityorboard.*' => 'required',
            'subject' => 'required|array',
            'subject.*' => 'required',
            'course_type.*' => 'required',
            'institution' => 'required|array',
            'institution.*' => 'required',
            'courseduration.*' => 'required',
            'percentage.*' => 'required',
            'passyear.*' => 'required|max:40',
            'quali_upload.*' => 'required|file|mimes:pdf|max:2048',
            'course.*' => [
                'nullable',
                'string',
            ],
            'specialization.*' => [
                'nullable',
                'string',
            ],
            'institution_additional.*' => [
                'nullable',
                'string',
            ],
            'upload_additional.*' => 'nullable|file|mimes:pdf|max:2048',
            'date_of_expiry.*' => 'nullable|date',
            'organization.*' => [
                'nullable',
                'string',
            ],
            'Designation.*' => [
                'nullable',
                'string',
            ],
            'jobrole.*' => [
                'nullable',
                'string',
            ],
            'responsibility.*' => [
                'nullable',
                'string',
            ],
            'fromdate.*' => [
                'nullable',
                'date',
            ],
            'todate.*' => [
                'nullable',
                'date',
            ],
            'uploadworkexperience.*' => 'nullable|mimes:pdf|max:2048', // maximum 2MB per file
            'inputPassport' => 'required|image|mimes:jpeg,jpg,png|max:1024',
            'identification_document' => 'required|mimes:pdf|max:1024',
            'inputSignature' => 'required|image|mimes:jpeg,jpg,png|max:50',
            'inputAddlSkill' => 'nullable|string',
            'inputAge' => 'nullable|integer|min:18|max:65',
            'inputDOB' => 'required|date_format:Y-m-d|before:' . now()->subYears(18)->format('Y-m-d'),
            'inputGender' => 'required|integer',
            'inputMobile' => 'required|integer|digits:10',
            'notification_id' => 'required|integer',
	    'captcha_response' => 'required|recaptcha',
        ], [
            'inputDOB.before' => 'You must be at least 18 years old. Please update the Date of Birth field',
            'inputAge.min' => 'The input age must be at least 18,please update Date of Birth field',
            'inputAge.max' => 'The input age must not be greater than 65,please update Date of Birth field',
            'identification_document.required' => 'Please upload an identification document in PDF less than 1 MB',
            'captcha_response.required' => 'Please check the captcha field',
        ]);
        if ($validator->fails()) {
            // Validation failed
            $errors = $validator->errors();
            $errorMessages = [];
            foreach ($errors->messages() as $field => $fieldErrors) {
                foreach ($fieldErrors as $errorMessage) {
                    $errorMessages[] = [
                        'message' => $errorMessage,
                        'field' => $field
                    ];
                }
            }
            return response()->json(['errors' => $errorMessages], 400);
        } else {
            return response()->json(['flag' =>1], 200);
        }

    }

But in my google captcha field the validation error could not show . But that works fine for other fileds with blade markup like

<div class="col-6 mb-3">
                    <label for="inputDistrict" name="inputDistrict" class="form-label" required>Select District</label>
                    <label class="error_cls">*</label>
                    <select class="form-select" name="inputDistrict" id="inputDistrict">
                        <option value="">Select District </option>
                    </select>
                </div>

My google recaptcha markup like

<div class="mb-3">
<div class="form-group">

                                            <div class="g-recaptcha" data-sitekey="6Ld6YCkpAAAAABNMxTK5YzUcCbW2VpbBGeqE6m0u"></div>
                                           @dump($errors)
                                          @if(isset($response['errors']))
            @foreach($response['errors'] as $error)
                @if($error['field'] === 'captcha_response')
                    <div class="alert alert-danger">{{ $error['message'] }}</div>
                @endif
            @endforeach
        @endif

            

       
                                        </div>

            </div>

But it's not working...

My Network console with that response can view as

alt text

Please advise I am stucking on this problem for 2 days.

Thanks

Anes P A

0 likes
4 replies
martinbean's avatar

@insight You’re re-inventing the wheel. Laravel already throws well-formed validation responses if validation fails. There’s absolutely no need to manually check if a validator’s failed and return your own validation response.

Snapey's avatar

You seem to be under the assumption that Blade will be re-evaluated AFTER your ajax call.

If you use ajax to query the server, you must deal with the errors in javascript. Expecting to use a blade @if statement is quite silly really if you think about it.

Please or to participate in this conversation.