gpt4 book ai didi

php - 在 Laravel 中使用表单请求时如何检查验证是否失败?

转载 作者:行者123 更新时间:2023-12-03 02:02:27 24 4
gpt4 key购买 nike

我正在尝试为 API 编写 CRUD。但是,当验证失败时,我不想将用户重定向到主页,而是返回带有错误的基于 json 的响应。

我可以使用以下代码来做到这一点

public function store(Request $request)
{
try {
$validator = $this->getValidator($request);

if ($validator->fails()) {
return $this->errorResponse($validator->errors()->all());
}

$asset = Asset::create($request->all());

return $this->successResponse(
'Asset was successfully added!',
$this->transform($asset)
);
} catch (Exception $exception) {
return $this->errorResponse('Unexpected error occurred while trying to process your request!');
}
}

/**
* Gets a new validator instance with the defined rules.
*
* @param Illuminate\Http\Request $request
*
* @return Illuminate\Support\Facades\Validator
*/
protected function getValidator(Request $request)
{
$rules = [
'name' => 'required|string|min:1|max:255',
'category_id' => 'required',
'cost' => 'required|numeric|min:-9999999.999|max:9999999.999',
'purchased_at' => 'nullable|string|min:0|max:255',
'notes' => 'nullable|string|min:0|max:1000',
];

return Validator::make($request->all(), $rules);
}

现在,我喜欢将一些代码提取到 form-request 中,以进一步清理我的 Controller 。我喜欢将我的代码更改为类似下面的代码。

public function store(AssetsFormRequest $request)
{
try {
if ($request->fails()) {
return $this->errorResponse($request->errors()->all());
}
$asset = Asset::create($request->all());

return $this->successResponse(
'Asset was successfully added!',
$this->transform($asset)
);
} catch (Exception $exception) {
return $this->errorResponse('Unexpected error occurred while trying to process your request!');
}
}

您可能会说 $request->fails()$request->errors()->all() 不起作用。如何检查请求是否失败,然后如何从表单请求中获取错误?

以下是我的 AssetsFormRequest 类的外观,供您引用

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

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

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|string|min:1|max:255',
'category_id' => 'required',
'cost' => 'required|numeric|min:-9999999.999|max:9999999.999',
'purchased_at' => 'nullable|string|min:0|max:255',
'notes' => 'nullable|string|min:0|max:1000',
];
}
}

最佳答案

在您的AssetFormRequest类中,您可以覆盖failedValidation方法到以下 -

public $validator = null;
protected function failedValidation(\Illuminate\Contracts\Validation\Validator $validator)
{
$this->validator = $validator;
}

然后你的 Controller 方法,用你的 $validator 对象做任何你想做的事情。可能类似于以下内容-

if (isset($request->validator) && $request->validator->fails()) {
return response()->json($request->validator->messages(), 400);
}

您可以看到this也可以链接以获取更多详细信息。希望它有帮助:)

关于php - 在 Laravel 中使用表单请求时如何检查验证是否失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48123558/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com