【问题标题】:Vue and Laravel Form Validation not returning 422 responseVue 和 Laravel 表单验证不返回 422 响应
【发布时间】:2019-03-10 04:28:37
【问题描述】:

我正在尝试使用 Laravel 和 Vue 在后端验证表单数据,但没有收到 422 响应。

这是在我的控制器功能中:

$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>

当我打开检查器并进入 Network->XHR 时,我会为 CRUD 返回所有正确的状态代码,但是当表单应该失败时,我不会返回 422 HTTP 状态代码。当我提交没有数据的表单时,它不保存,但没有发送状态码,没有响应给用户反馈。感谢您的帮助。

【问题讨论】:

  • 你在 XHR 中的反应是什么?
  • 当我点击保存并故意验证失败时,什么都没有发生,我没有得到回应,没有反馈。
  • 如果没有响应,你怎么能说“验证失败”?

标签: javascript ajax laravel validation vue.js


【解决方案1】:
$this->validate($request, [
    'name' => 'required',
    'city' => 'required',
    'state' => 'required'
    ]);

我从未见过这样的用法。我认为控制器没有验证功能。试试这个:

$request->validate([
    'name' => 'required',
    'city' => 'required',
    'state' => 'required'
    ]);

【讨论】:

  • 我刚刚检查过,是的,有一个类似来自 ValidateRequest 特征的用法,但该函数没有抛出任何异常。它只返回一个未经验证的字段数组。看; laravel.com/api/5.5/Illuminate/Foundation/Validation/…
  • 我进行了更改,但结果仍然相同。实际上我确实得到了 302 的状态,当我点击保存时输入 text/html。我不应该得到422吗?它与表单上的 v-model="location.name" 有什么关系,但我只在控制器中引用“name”?
  • 这可能是因为向新模型实例添加了 id 字段。不要在你的 laravel 模型实例中使用任何 id 修改,让框架自己处理。我的意思是,仅将 id 用于 findOrFail 方法。
  • 你能克隆这个项目并尝试添加后端验证吗? github.com/bradtraversy/larticles_vue_app 我尝试得到 302 而不是 422
【解决方案2】:

我必须在 addLocation 方法的标头中添加 "accept": "application/json"。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-01
    • 2021-06-06
    • 1970-01-01
    • 2020-09-03
    • 2020-10-09
    • 2017-05-02
    • 2020-01-17
    • 2018-08-30
    相关资源
    最近更新 更多