简体   繁体   English

使用Vue JS进行Laravel验证

[英]Laravel Validation with vue js

i want to post ajax request using vue-resource this.$http.post request. 我想使用vue-resource this.$http.post请求发布ajax请求。 it worked perfectly fine if i passed all validation rules but i want to get some validations if it fails. 如果我通过了所有验证规则,它工作得很好,但是如果失败了,我想进行一些验证。 so far i keep getting 500 error if i don't fill out some input fields. 到目前为止,如果我不填写某些输入字段,我会不断收到500错误。 it's hard for me to debug the error because it didn't appeared on the network tab. 我很难调试该错误,因为它没有出现在“网络”选项卡上。

here's what i've done so far 这是我到目前为止所做的

//my modal component
<script>
export default {
    props: ['show'],

    data() {
        return {
            input: {
                id: '',
                name: '',
                address: '',
                email: ''
            },
            errorInputs: {}
        }
    },

    methods: {
        createStudent() {
            this.$http.post('/students', this.$data.input)
                .then((response) => {
               alert('added new row!)
            }, (response) => {
                console.log(response.data);
            });
        }
      }
   }
</script>

// my controller

public function store(Request $request) {
    $validator = $this->validate($request,[
        'id' => 'required',
        'name' => 'required|unique:students',
        'email' => 'required|unique:students|email',
        'address' => 'required',
    ]);

    if($validator->passes()){
        Student::create($request->all());

        return response()->json([], 201);
    }

    $errors = json_decode($validator->errors());

    return response()->json([
        'success' => false,
        'message' => $errors
    ],422);
}

any helps and references would be appreciated. 任何帮助和参考将不胜感激。 i am using laravel 5.3 and vue js 2 我正在使用laravel 5.3和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). $this->validate()返回422错误响应以及您的验证错误,因此您应该在then()第二个回调中获得这些错误(就像您现在所做的那样)。 Your vue component body should be like this: 您的vue组件主体应如下所示:

{
    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. 记住要在输入中添加v-model="input.fieldName"属性。

Remember to include your session token along with your post, unless of course you are disabling csrf tokens for that route. 请记住,除非您要为该路由禁用csrf令牌,否则请在会话中包括会话令牌。

Since Laravel 5.1 you can disable this in your verifytoken middleware 从Laravel 5.1开始,您可以在verifytoken中间件中禁用此功能

 <?php namespace App\Http\Middleware;

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

class VerifyCsrfToken extends ...    {
  protected $except = [
    'payment/*',
  ];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM