alxy / oc-captcha-plugin

Integrates Googles reCAPTCHA into October.
13 stars 7 forks source link

OctoberCMS custom contact form with Captcha (Alxy captcha) plugin #7

Open mittultechnobrave opened 7 years ago

mittultechnobrave commented 7 years ago

I have created a custom contact form with an ability to send emails.

Everything working fine as I am able to do validations as well as emails getting sent using SMTP.

The thing is, now I want to add captcha (Google Captcha) into my custom form, hence I have used Alxy Captcha plugin, installed it and followed the things which mentioned in their document.

I have also created their respective keys and I am able to see captcha if I run my URL in browser.

But I am unable to handle captcha server validations into it. How can I do this ?

If I click without filling recaptcha, I am getting error saying

["missing-input-response"]

If I fill recaptcha, it works fine as I am able to send emails like before.

Here below are my screenshots what I have done so far in my code for better idea.

1) My Contact Form Partial (Snippet):

enter image description here

2) My Contact Form Static Page (Backend):

enter image description here

3) Contact Form Front-end Preview:

enter image description here

4) Default Layout Markup:

enter image description here

5) Default Layout Code:

enter image description here

As you can see, here I want to do server side validations, for other fields it works fine. But for recaptcha, I do not know how to validate it if its filled or not.

What should I do from here on to make it work?

Also I want to reset recaptcha in my clearAllInputs() javascript function if success.

Any idea or thoughts ?

Thanks

alxy commented 7 years ago

You need to know that the middleware always runs before the request is routed or processed: https://github.com/alxy/oc-captcha-plugin/blob/master/middleware/CaptchaMiddleware.php#L20

If you need a second validation, you can validate that post('g-recaptcha-response') does exist. However, I don't see why it isn't properly converted to an AjaxException, but just holds that plain value. (The error code is correct though, as there is really no data, if the user doesnt check the box) I don't have too much time right now, but maybe you can somehow investigate, why the error isn't correctly fed to this class and can thus not be correctly handled by the ajax framework. Note, that you still need to handle errors somehow clientside, as the CAPTCHA validation runs first and will thus abort the entire processing flow (i.e. if the CAPTCHA isnt filled, your server side validation code should not be touched at all.)