laravel - user php captcha in vuejs component -
in laravel 5.4.20 , vuejs. want use simple code captcha in ue modal component (i don't want use recaptcha). how can this?
i'm using mewebstudio captcha this:
route::any('captcha-test', function() { if (request::getmethod() == 'post') { $rules = ['captcha' => 'required|captcha']; $validator = validator::make(input::all(), $rules); if ($validator->fails()) { echo '<p style="color: #ff0000;">incorrect!</p>'; } else { echo '<p style="color: #00ff30;">matched :)</p>'; } } $form = '<form method="post" action="captcha-test">'; $form .= '<input type="hidden" name="_token" value="' . csrf_token() . '">'; $form .= '<p>' . captcha_img() . '</p>'; $form .= '<p><input type="text" name="captcha"></p>'; $form .= '<p><button type="submit" name="check">check</button></p>'; $form .= '</form>'; return $form; }); but work in php documents (i need in vue component).
to achieve that, route this:
route::post('captcha-test', function() { $validator = validator::make(input::all(), [ 'captcha' => 'required|captcha' ])->validate(); return response(['status' => 'ok'], 200); }); and vue component, this:
vue.component('captcha', { props: ['image'], data: function () { return { form: { captcha: null }, isworking: false } }, methods: { check: function () { var self = self.isworking = true axios.post('/captcha-test', { captcha: self.form.captcha }) .then(function (response) { // valid captcha... console.log(response); // reset form self.form.captcha = null // now, not working self.isworking = false }) .catch(function (err) { // invalid captch.. console.log(err); // reset form self.form.captcha = null // now, not working self.isworking = false }); } } }); new vue({ el: '#app' }); <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.16.2/axios.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.5.1/css/bulma.css" rel="stylesheet"/> <div id="app" class="section"> <captcha image="<img src='https://c22blog.files.wordpress.com/2010/10/input-black.gif' style='width: 150px;'>" inline-template> <div class="box"> <div v-html="image"></div> <form @submit.prevent="check"> <div class="field"> <div class="control"> <input class="input" type="text" placeholder="captcha" v-model="form.captcha" required> </div> </div> <div class="field"> <div class="control"> <button type="submit" class="button is-primary" :class="{'is-loading': isworking}" :disabled="isworking">check</button> </div> </div> </form> </div> </captcha> </div>
Comments
Post a Comment