albertcht / invisible-recaptcha

An invisible reCAPTCHA package for Laravel, Lumen, CI or native PHP.
MIT License
603 stars 163 forks source link

"g-recaptcha-response field is required" - Field is empty #86

Closed ColinTravis closed 6 years ago

ColinTravis commented 6 years ago

Hello, I'm on the newest commit currently of this package. I can see the captcha badge, and my _submitForm/_captchaForm/_captchaSubmit all bind properly. However, every time I submit my form it doesn't also submit the g-recaptcha-response field, causing validation to fail.

I've tried moving the captcha render around within the form, to see if it was an issue with where it loaded, as well as tried to pull the value before submit and I've tried to use a custom submit function as well.

albertcht commented 6 years ago

Hi @ColinTravis ,

Can you try to upgrade your package to release v1.8.3 to see if this issue still exists?

ColinTravis commented 6 years ago

Just fully updated, issue still persists. I've read around that with captcha, sometimes it's position within a form can prevent it from functioning, so I moved it around again to see if that fixed it. I think is should be fine where it is now (right before the form ends, and still within the input groups)

<div class="col-md-12 mb-5">
<h4 class="footer-nav email-subscribe-label mb-2">Subscribe To Our Newsletter</h4>
<form method="post" action="{{ route('email.subscribe') }}" class="form-inline" id="newsletter-subscribe-form">
                     {{ csrf_field() }}
                         <div class="input-group">
                              <input type="hidden" name="item" value="newsletter" />
                               <input type="email" class="form-control email-subscribe" placeholder="Email" name="EmailAddress" id="newsletter-email-address" />
                                <span class="input-group-btn">
                                <button id="newsletter-subscribe" type="submit" class="btn btn-form btn orange">SUBSCRIBE</button>
                                 </span>
                                    @captcha
                                </div>
                            </form>
                        </div>

There'a also nothing too crazy going on in my mail controller:

     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function subscribe(Request $request)
    {
        $subscribeConfig = config('campaignmonitor.subscribe.' . $request->get('item'));
        $campaignMonitorData = $request->except(['item', 'ResponseMessage']);

        $validateFields = array('g-recaptcha-response' => 'required|captcha');
        $this->validate($request, $validateFields);

        if(!empty($subscribeConfig['customFields'])) {
            if(empty($campaignMonitorData['CustomFields']))
                $campaignMonitorData['CustomFields'] = [];
            foreach($subscribeConfig['customFields'] as $key => $value) {
                $campaignMonitorData['CustomFields'][] = [
                    'Key' => $key,
                    'Value' => $value,
                ];
            }
        }

        $listID = $subscribeConfig['listID'];
        $result = \CampaignMonitor::subscribers($listID)->add($campaignMonitorData);

        if($request->ajax()) {
          return response()->json($result);
        }
        $message = $request->get('ResponseMessage', 'Thank you!');
        return view('emails.thank-you', ['message' => $message]);
    }
albertcht commented 6 years ago

The @captcha directive should be placed within a form element. So does it work now?

ColinTravis commented 6 years ago

No, it still doesn't validate, as the g-captcha field still ends up being blank when it submits.

albertcht commented 6 years ago

Can you please try to reproduce your issue on codepen? Like what I did here: https://codepen.io/albertcht/pen/MXraNr, that would be easier to debug. I only need your html redered by your frontend code.