[Vuejs]-Laravel Validation with vue js

2๐Ÿ‘

โœ…

$this->validate() returns 422 error response alongside your validation errors, so you should get those errors in then() second callback (like you do now). Your vue component body should be like this:

{
    data() {
        // ...
    },
    createStudent() {
        this.$http
            .post('/students', this.input)
            .then(this.handleSuccess, this.handleError)
    },
    handleSuccess(res) {
        alert('student created')
    },
    handleError(res) {
        if (res.status === 422) {
            this.errorInputs = res.body
        } else {
            alert('Unkown error!')
        }
    }
}

Remember to add v-model="input.fieldName" properties to your inputs.

๐Ÿ‘คSkysplit

0๐Ÿ‘

Remember to include your session token along with your post, unless of course you are disabling csrf tokens for that route.

Since Laravel 5.1 you can disable this in your verifytoken middleware

 <?php namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as ...

class VerifyCsrfToken extends ...    {
  protected $except = [
    'payment/*',
  ];
}
๐Ÿ‘คAlex

Leave a comment