Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

captcha form validation required error message in vue-recaptcha

I am following this Package in vue.js and Laravel 5.6.7 to implement captcha.

https://github.com/DanSnow/vue-recaptcha#install

Component code in vue.js

<template>
    <div>        
        <vue-recaptcha  v-model="loginForm.recaptcha"
            sitekey="My key">
        </vue-recaptcha> 

        <button type="button" class="btn btn-primary">
            Login
        </button>                            
    </div>
</template>

<script>

</script>

app.js code

import VueRecaptcha from 'vue-recaptcha';
Vue.use(VeeValidate);
Vue.component('vue-recaptcha', VueRecaptcha);

Question:

Is there any property for vue-recaptcha called required which can be passed to show the form validation message?

like image 907
Pankaj Avatar asked Mar 19 '18 13:03

Pankaj


People also ask

How do I fix failed to validate a Google reCAPTCHA token?

There are a few steps you can take to improve your experience: Make sure your browser is fully updated (see minimum browser requirements) Check that JavaScript is enabled in your browser. Try disabling plugins that might conflict with reCAPTCHA.


1 Answers

You can use a property (loginForm.recaptchaVerified below) to track if the recaptcha was verified and prevent submit + display a message if not:

JSFiddle demo: https://jsfiddle.net/acdcjunior/o7aca7sn/3/

Code below:

Vue.component('vue-recaptcha', VueRecaptcha);

new Vue({
  el: '#app',
  data: {
    loginForm: {
      recaptchaVerified: false,
      pleaseTickRecaptchaMessage: ''
    }
  },
  methods: {
    markRecaptchaAsVerified(response) {
      this.loginForm.pleaseTickRecaptchaMessage = '';
      this.loginForm.recaptchaVerified = true;
    },
    checkIfRecaptchaVerified() {
      if (!this.loginForm.recaptchaVerified) {
        this.loginForm.pleaseTickRecaptchaMessage = 'Please tick recaptcha.';
        return true; // prevent form from submitting
      }
      alert('form would be posted!');
    }
  }
})
<script src="https://www.google.com/recaptcha/api.js?onload=vueRecaptchaApiLoaded&render=explicit" async defer>
</script>
<script src="https://unpkg.com/vue"></script>
<script src="https://unpkg.com/vue-recaptcha@latest/dist/vue-recaptcha.js"></script>

<div id="app">
  <form v-on:submit.prevent="checkIfRecaptchaVerified">
     <div>
        <vue-recaptcha @verify="markRecaptchaAsVerified"
            sitekey="6Le-wvkSAAAAAPBMRTvw0Q4Muexq9bi0DJwx_mJ-">
        </vue-recaptcha>
     </div>
     Some other fields of the form here...
     <br>
     <button>Submit form</button>
     <hr>
     <div><strong>{{ loginForm.pleaseTickRecaptchaMessage }}</strong></div>
  </form>
</div>
like image 79
acdcjunior Avatar answered Sep 19 '22 01:09

acdcjunior