gpt4 book ai didi

php - 如何在 Laravel 中自定义错误消息?

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

这是我的代码:

public function store(Request $request){

$this->validate($request, [
'name' => 'required',
'email' => 'required|email',
'content' => 'required|min:10',
]);

$new_array = array();
$new_array['name'] = $request->name;
$new_array['email'] = $request->email;
$new_array['content'] = $request->content;
contact::create($new_array);

return back();
}

现在我需要为这些过滤器设置一些新的错误消息(例如: requiredemail 、..)。

当前,这是 required 的默认错误消息:

The name field is required.



我怎样才能改变它?

最佳答案

箱子规则

public function user_registration_rules(array $data)
{
$messages = [
'full-name.required' => 'Please enter full name',
'address.required' => 'Please enter address'
];

$validator = Validator::make($data, [
'full-name' => 'required|min:5|max:70',
'address' => 'required'
], $messages);

return $validator;
}

像这样在 Controller 中使用
$validator = $this->user_registration_rules($request_data);   
if($validator->fails())
{
return redirect()->back()->withErrors($validator)->withInput();
}

关于php - 如何在 Laravel 中自定义错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40124518/

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