我无法在我的Laravel应用中检索到的vue组件中显示错误消息。
这是“网络”标签中的响应
{message: "The given data was invalid.",…}
errors: {results.0.winner: ["This field is required"], results.1.winner: ["This field is required"],…}
results.0.winner: ["This field is required"]
results.1.winner: ["This field is required"]
results.2.winner: ["This field is required"]
message: "The given data was invalid."
这是控制台中的错误
admin.js:59648 [Vue警告]:渲染错误:“ TypeError:无法读取 未定义的属性“ 0””
这是我的组成部分
<template>
<form @submit.prevent="submit" class="mb-5">
<div class="row">
<div v-for="(setting, index) in settings">
<div class="card">
<div class="card-body">
<div class="form-group">
<input type="text" class="form-control" v-model="setting.number">
<div v-if="errors && errors.results[index].winner" class="text-danger">{{ errors.results[index].winner }}</div>
</div>
</div>
</div>
</div>
</div>
<div class="form-group row">
<div class="col-sm-8">
<button type="submit" class="btn btn-primary">
Save Results
</button>
</div>
</div>
</form>
</template>
<script>
export default {
props: {
settings: {
type: Array,
default: () => []
},
},
data: function() {
return {
errors: {},
}
},
mounted() {
},
methods: {
submit: function() {
let results = [];
this.settings.forEach(function(item) {
results.push({
prize: item.prize,
winner: item.number
});
});
axios.post('save', {results: results}).then(response => {
if(response.status === 200) {
//window.location.href = '/admin/lottery'
}
}).catch(error => {
if (error.response.status === 422) {
this.errors = error.response.data.errors || {}
}
});
},
},
};
</script>
如果它有助于我的laravel功能
public function store(Request $request)
{
$request->validate([
'results.*.winner' => 'required|gt:0',
],
[
'results.*.winner.required' => 'This field is required',
]);
}
答案 0 :(得分:0)
您使用的索引键
errors.results[index].winner
它属于可迭代变量设置,尽管您确定可以确定交集匹配,但不建议这样做,我认为这是“渲染错误”。
您必须编码:
<div v-for="(result, index) in errors.results">
<div class="card">
<div class="card-body">
<div class="form-group">
<input type="text" class="form-control" v-model="setting.number">
<div class="text-danger">{{ result.winner }}</div>
</div>
</div>
</div>
</div>
代替:
<div v-for="(setting, index) in settings">
<div class="card">
<div class="card-body">
<div class="form-group">
<input type="text" class="form-control" v-model="setting.number">
<div v-if="errors && errors.results[index].winner" class="text-danger">{{ errors.results[index].winner }}</div>
</div>
</div>
</div>
</div>