php 自定义 Laravel 验证消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45007905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom Laravel validation messages
提问by YVS1102
I'm trying to create customized messages for validation in Laravel 5. Here is what I have tried so far:
我正在尝试在 Laravel 5 中创建用于验证的自定义消息。这是我迄今为止尝试过的:
$messages = [
'required' => 'Harap bagian :attribute di isi.',
'unique' => ':attribute sudah digunakan',
];
$validator = Validator::make($request->all(), [
'username' => array('required','unique:Userlogin,username'),
'password' => 'required',
'email' => array('required','unique:Userlogin,email'),$messages
]);
if ($validator->fails()) {
return redirect('/')
->withErrors($validator) // send back all errors to the login form
->withInput();
} else {
return redirect('/')
->with('status', 'Kami sudah mengirimkan email, silahkan di konfirmasi');
}
But it's not working. The message is still the same as the default one. How can I fix this, so that I can use my custom messages?
但它不起作用。该消息仍与默认消息相同。我该如何解决这个问题,以便我可以使用我的自定义消息?
回答by Zaheer Attar
If you use $this->validate()simplest one, then you should write code something like this..
如果你使用$this->validate()最简单的,那么你应该写这样的代码..
$rules = [
'name' => 'required',
'email' => 'required|email',
'message' => 'required|max:250',
];
$customMessages = [
'required' => 'The :attribute field is required.'
];
$this->validate($request, $rules, $customMessages);
回答by Jankyz
Laravel 5.7.*
Laravel 5.7.*
Also You can try something like this. For me is the easiest way to make custom messages in methods when you want to validate requests:
你也可以尝试这样的事情。对我来说,当您想验证请求时,在方法中制作自定义消息的最简单方法:
public function store()
{
request()->validate([
'file' => 'required',
'type' => 'required'
],
[
'file.required' => 'You have to choose the file!',
'type.required' => 'You have to choose type of the file!'
]);
}
回答by Jigar Shah
You can provide custom message like :
您可以提供自定义消息,例如:
$rules = array(
'URL' => 'required|url'
);
$messages = array(
'URL.required' => 'URL is required.'
);
$validator = Validator::make( $request->all(), $rules, $messages );
if ( $validator->fails() )
{
return [
'success' => 0,
'message' => $validator->errors()->first()
];
}
or
或者
The way you have tried, you missed Validator::replacer(), to replace the :variable
您尝试过的方式,您错过了Validator::replacer(),以替换:variable
Validator::replacer('custom_validation_rule', function($message, $attribute, $rule, $parameters){
return str_replace(':foo', $parameters[0], $message);
});
回答by pgrono
$ruls = [
'username' => 'required,unique:Userlogin,username',
'password' => 'required',
'email' => 'required,unique:Userlogin,email'
]);
$messages = [
'required' => 'Harap bagian :attribute di isi.',
'unique' => ':attribute sudah digunakan'
];
$request->validate($ruls,$messages);
//only if validation success code below will be executed

