自定义消息不会基于Laravel 9.8.1的formrequest返回。
Next is my FormRequest:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreFruitRequest 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:3',
'carbohydrates' => 'required|numeric|gte:0',
'protein' => 'required|numeric|gte:0',
'fat' => 'required|numeric|gte:0',
'calories' => 'required|integer|numeric|gte:0',
'sugar' => 'required|numeric|gte:0',
];
}
/**
* Error Messages
*
* @return array
*/
public function messages() :array
{
return [
'carbohydrates.gte' => 'Nutrients cannot be negative',
'protein.gte' => 'Nutrients cannot be negative',
'fat.gte' => 'Nutrients cannot be negative',
'calories.gte' => 'Nutrients cannot be negative',
'sugar.gte' => 'Nutrients cannot be negative',
];
}
}
The validation rules work fine but when I make the request with the wrong value, which is a negative amount, validation does not return these messages to the controller.因此,我的API返回主叶片页面作为错误。
我的代码中有什么不正确? Or, do I need some tunings anywhere?
Controller (as an answer for @NickSdot):
/**
* Store a newly created resource in storage.
*
* @param StoreFruitRequest $request
* @return JsonResponse
*/
public function store(StoreFruitRequest $request)
{
return response()->json(Fruit::create($request->all()) ?? "Cannot create fruit $request->name");
}
Next is my FormRequest:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreFruitRequest 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:3',
'carbohydrates' => 'required|numeric|gte:0',
'protein' => 'required|numeric|gte:0',
'fat' => 'required|numeric|gte:0',
'calories' => 'required|integer|numeric|gte:0',
'sugar' => 'required|numeric|gte:0',
];
}
/**
* Error Messages
*
* @return array
*/
public function messages() :array
{
return [
'carbohydrates.gte' => 'Nutrients cannot be negative',
'protein.gte' => 'Nutrients cannot be negative',
'fat.gte' => 'Nutrients cannot be negative',
'calories.gte' => 'Nutrients cannot be negative',
'sugar.gte' => 'Nutrients cannot be negative',
];
}
}
The validation rules work fine but when I make the request with the wrong value, which is a negative amount, validation does not return these messages to the controller. So, my API returns the main blade page as an error.
What is incorrect in my code? Or, do I need some tunings anywhere?
Controller (as an answer for @NickSdot):
/**
* Store a newly created resource in storage.
*
* @param StoreFruitRequest $request
* @return JsonResponse
*/
public function store(StoreFruitRequest $request)
{
return response()->json(Fruit::create($request->all()) ?? "Cannot create fruit $request->name");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在中间件文件夹中添加一个名为forcejsonresponse.php的中间件,然后将其添加到$ MIDDERWARE变量内的kernel.php中。
ForceJsonResponse.php
Kernel.php
You can add a middleware called ForceJsonResponse.php inside Middleware folder, and then add it to Kernel.php inside the $middleware variable.
ForceJsonResponse.php
Kernel.php