Pan*_*kaj 3 vue.js laravel-5.6
我正在vue.js和Laravel 5.6.7中遵循此Package来实现验证码。
https://github.com/DanSnow/vue-recaptcha#install
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>
Run Code Online (Sandbox Code Playgroud)
app.js代码
import VueRecaptcha from 'vue-recaptcha';
Vue.use(VeeValidate);
Vue.component('vue-recaptcha', VueRecaptcha);
Run Code Online (Sandbox Code Playgroud)
题:
vue-recaptcha是否有任何需要的属性,可以传递以显示表单验证消息?
您可以使用loginForm.recaptchaVerified下面的属性来跟踪验证码是否已通过验证,如果没有验证,则阻止提交+显示消息:
JSFiddle演示:https ://jsfiddle.net/acdcjunior/o7aca7sn/3/
代码如下:
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!');
}
}
})
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)