在laravel 5.4 + vueJS中处理表单验证错误

时间:2017-06-06 02:32:26

标签: laravel vue.js laravel-5.4 vue-router

我有这样的形式create.vue:

    <template>
    <div>
        <div v-if="message" class="alert alert-success">
            {{ message }}
        </div>

        <form @submit.prevent="store" action="/user" method="post">
            <div :class="['form-group', errors.name ? 'has-error' : '']">
            <!--<div class="form-group">-->
            <label>Full name</label>
                <input v-model="state.name" type="text" class="form-control">
                <span v-if="errors.name" class="label label-danger">{{ errors.name[0] }}</span>
                <!--<span class="label label-danger">{{ errors(errors.name) }}</span>-->
            </div>

            <div :class="['form-group', errors.email ? 'has-error' : '']">
            <!--<div class="form-group">-->
                <label>Email</label>
                <input v-model="state.email" type="email" class="form-control">
                <span v-if="errors.email" class="label label-danger">{{ errors.email[0] }}</span>
                <!--<span class="label label-danger">{{ errors(errors.email) }}</span>-->
            </div>

            <div class="form-group">
                <button class="btn btn-primary">Submit</button>
                <!--<router-link :to="{ name: 'userIndex' }">Back to index</router-link>-->
            </div>
        </form>
    </div>
</template>

<script>
export default {
    data() {
        return {
            errors: [],
            user: [],
            state: {
                name: '',
                email: ''
            }
        }
    },

    methods: {
        store(e) {
            axios.post(e.target.action, this.state).then(response => {
                this.errors = [];

                this.state = {
                    name: '',
                    email: ''
                }

                this.message = 'User has been created.';
            }).catch(error => {
                if (! _.isEmpty(error.response)) {
                    if (error.response.status = 422) {
                        this.errors = error.response.data;
                    }
                }
            });
        }
    }
}
</script>

我的控制器: UserCOntroller.php

public function store(Request $request)
     {
         $this->validate($request, [
             'name'  => 'required|string|max:50',
             'email' => 'required|email|unique:users,email',
         ]);

         $user = User::create([
             'name'     => $request->name,
             'email'    => $request->email,
             'password' => bcrypt(str_random()),
         ]);

         return response()->json($user);
     }

当我键入类似于以前存在的内容(唯一)时,我在控制台响应中获得错误文本,但不像往常那样以表单形式显示。我在控制台中遇到这样的错误:

{"email":["The email has already been taken."]}

如何以表格形式显示? 我的剧本出了什么问题?

任何帮助?谢谢你......

1 个答案:

答案 0 :(得分:1)

您好我尝试在我的本地复制您的脚本。虽然在我身上有用。你能尝试调试catch中的对象错误吗?发送样本结果:)

if (error.response.status = 422) {
    this.errors = error.response.data;
    console.log(this.errors);                             
}