【问题标题】:Laravel 5.8 Custom Form Request not working as intended?Laravel 5.8 自定义表单请求未按预期工作?
【发布时间】:2019-11-23 03:48:17
【问题描述】:

我使用以下代码创建了一个名为 ClientStoreRequest 的自定义表单请求:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ClientStoreRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
        // return  $this->user()->can('add-clients');
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required|unique:clients|max:255',
            'website' => 'required|url',
            'street' => 'required',
            'town' => 'required',
            'postcode' => 'required|max:8',
            'county' => 'required',
            'country' => 'required'
        ];
    }
}

然后我将ClientControllerstore 方法修改为如下所示:

    /**
     * Store a newly created resource in storage.
     *
     * @param  ClientStoreRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(ClientStoreRequest $request)
    {
        dd(1);
    }

所以当表单提交时,它应该杀死页面并在屏幕上打印一个1。当我使用ClientStoreRequest $request 时,它只会将我发送回我提交表单的页面,没有错误且没有dd(1) 结果,但是当我使用Request $request 时,它会将1 打印到屏幕上。

我是否遗漏了一些非常明显的东西?我有 followed the docs 来做这个。

编辑:我正在使用资源控制器,所以路由是Route::resource('clients', 'ClientController');

【问题讨论】:

  • 你的路线是怎么设置的?
  • laravel.com/docs/5.8/validation#form-request-validation >> 当您为请求定义规则时,您的控制器功能将不会被触及,因为您的规则不允许运行它。您是否发送了所有必需的参数?
  • @RobBiermann 我不明白你的意思,我现在就发布路线

标签: php laravel laravel-5 laravel-5.8 laravel-request


【解决方案1】:

有点尴尬,但这完全是由于开发人员的错误。自定义表单请求实际上工作正常......只是我的规则引用了我忘记放入表单的一个字段,因此没有在屏幕上显示错误!一旦我回显了通常的 $errors 数组,我就可以看到我的错误 - 我会责怪深夜编码!

【讨论】:

  • name="" 属性,使规则不起作用..
猜你喜欢
  • 1970-01-01
  • 2022-11-11
  • 2018-03-09
  • 2015-04-11
  • 2018-04-24
  • 1970-01-01
  • 1970-01-01
  • 2017-01-01
  • 2021-10-22
相关资源
最近更新 更多