【问题标题】:Custom Request for Form Validations表单验证的自定义请求
【发布时间】:2020-07-28 13:21:58
【问题描述】:

我遇到了自定义请求的问题。我用php artisan make:request StoreNewClient 创建了一个请求。

我已经在新的请求文件中配置了验证逻辑,比如:

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreNewClient extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return Auth::check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [ ...
        ];
    }

    /**
     * Get the error messages for the defined validation rules.
     *
     * @return array
     */
    public function messages()
    {
        return [ ...
        ];
    }
}

在控制器中,我导入了 use App\Http\Requests\StoreNewClient; 之类的文件,之后,在函数 store() 中我写了:

public function store(StoreNewClient $request)
{
// Will return only validated data        
$validated = $request->validated();

...
}

这是我从文档中了解到的,但这给了我一个错误:Class App\Http\Requests\StoreNewClient does not exist but exists (!!)。

我已经尝试清除缓存并转储作曲家,但没有解决问题。有什么帮助吗?

【问题讨论】:

    标签: php laravel forms request


    【解决方案1】:

    发现错误。在 messages() 中,1 行末尾没有逗号,应用程序和 composer dump-autoload 都没有给出任何错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-11
      • 2019-11-27
      • 2015-09-24
      • 2017-12-06
      • 2017-05-30
      • 2023-03-29
      • 2018-05-31
      • 2017-11-17
      相关资源
      最近更新 更多