Javascript Vue和Laravel表单验证未返回422响应
我试图用Laravel和Vue在后端验证表单数据,但没有得到422响应 这在我的控制器功能中:Javascript Vue和Laravel表单验证未返回422响应,javascript,ajax,laravel,validation,vue.js,Javascript,Ajax,Laravel,Validation,Vue.js,我试图用Laravel和Vue在后端验证表单数据,但没有得到422响应 这在我的控制器功能中: $this->validate($request, [ 'name' => 'required', 'city' => 'required', 'state' => 'required' ]); $location = $request->isMethod('put') ? Locations::findOrFail($
$this->validate($request, [
'name' => 'required',
'city' => 'required',
'state' => 'required'
]);
$location = $request->isMethod('put') ?
Locations::findOrFail($request->id) : new Locations;
$location->id = $request->input('id');
$location->name = $request->input('name');
$location->address = $request->input('address');
$location->city = $request->input('city');
$location->state = $request->input('state');
$location->zip = $request->input('zip');
$location->phone = $request->input('phone');
if($location->save())
{
return new LocationsResource($location);
}
以下是Vue方法:
addLocation() {
if (this.edit === false) {
// Add
fetch('api/location', {
method: 'post',
body: JSON.stringify(this.location),
headers: {
'content-type': 'application/json'
}
})
.then(res => res.json())
.then(data => {
this.clearForm();
alert('Location Added');
this.fetchArticles();
});
} else {
// Update
fetch('api/location', {
method: 'put',
body: JSON.stringify(this.location),
headers: {
'content-type': 'application/json'
}
})
.then(res => res.json())
.then(data => {
this.clearForm();
alert('Locations Updated');
this.fetchArticles();
})
}
}
表格如下:
<form @submit.prevent="addLocation" class="mb-3">
<div class="form-group">
<input type="text" class="form-control" placeholder="Name" v-model="location.name">
<input type="text" class="form-control" placeholder="City" v-model="location.city">
<input type="text" class="form-control" placeholder="State" v-model="location.state">
</div>
<button type="submit" class="btn btn-light btn-block">Save</button>
</form>
拯救
当我打开inspector并进入Network->XHR时,我得到了CRUD的所有正确状态代码,但是当表单应该失败时,我没有得到422 HTTP状态代码。当我在没有数据的情况下提交表单时,它不会保存,但不会发送状态代码,也不会向用户提供反馈。谢谢你的帮助
$this->validate($request, [
'name' => 'required',
'city' => 'required',
'state' => 'required'
]);
我从未见过这样的用法。我认为控制器没有验证功能。试试这个:
$request->validate([
'name' => 'required',
'city' => 'required',
'state' => 'required'
]);
我从未见过这样的用法。我认为控制器没有验证功能。试试这个:
$request->validate([
'name' => 'required',
'city' => 'required',
'state' => 'required'
]);
我必须在addLocation方法的标题中添加“accept”:“application/json”。我必须在addLocation方法的标题中添加“accept”:“application/json”。您的响应在XHR中说了什么?当我单击save并故意使验证失败时,没有任何事情发生,我没有得到响应,没有反馈。如果没有响应,你怎么能说“验证失败”?你在XHR中的响应是什么?当我单击“保存”并故意使验证失败时,什么都没有发生,我没有得到响应,没有反馈。如果没有响应,你怎么能说“验证失败”呢?我刚刚检查了一下,是的,ValidateRequest trait中有类似的用法,但没有在该函数中引发任何异常。它只返回一个未验证字段数组。看见我做了改变,但结果还是一样。实际上,我的状态是302,单击save时键入text/html。我应该买422吗?它是否与表单上的v-model=“location.name”有关,但我只在控制器中引用“name”?这可能是因为向新模型实例添加了id字段。不要在laravel模型实例中使用任何id修改,让框架自行处理。我的意思是,使用id just for findOrFail方法。你能克隆这个项目并尝试添加后端验证吗?我试着得到了一个302而不是422,我刚刚检查过,是的,ValidateRequest trait中有这样一个用法,但是在该函数中没有抛出任何异常。它只返回一个未验证字段数组。看见我做了改变,但结果还是一样。实际上,我的状态是302,单击save时键入text/html。我应该买422吗?它是否与表单上的v-model=“location.name”有关,但我只在控制器中引用“name”?这可能是因为向新模型实例添加了id字段。不要在laravel模型实例中使用任何id修改,让框架自行处理。我的意思是,使用id just for findOrFail方法。你能克隆这个项目并尝试添加后端验证吗?我试着用302而不是422