如何使用 Laravel 规则设置 Laravel 自定义验证消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47918872/
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
How to set laravel custom validation message with laravel rules
提问by Javed
Let me show first my code. Here is my controller function code
让我先展示我的代码。这是我的控制器功能代码
public function save(Request $request) {
try {
$this->validate($request, Venue::rules()); // Validation Rules
$venue = Venue::saveOrUpdate($request);
if($venue !== false) {
if($request->get('continue', false)) {
return redirect()->route('admin.venue.edit', ['id' => $venue->id])->with('success', trans('admin.venue.save_success'));
} else {
return redirect()->route('admin.venue.index')->with('success', trans('admin.venue.save_success'));
}
} else {
return back()->with('error', "Unable to save venue")->withInput();
}
} catch (\Exception $ex) {
return back()->with('error', "Unable to save venue")->withInput();
}
}
Here is my model function code
这是我的模型功能代码
public static function rules($id = '') {
return [
'name' => 'required|string|max:255',
'logo' => 'required',
'status' => 'required|string|in:' . implode(",", Venue::STATUSES),
'venue_type_id' => 'required|string|not_in:0',
'client_id' => 'required|string|not_in:0',
];
}
So now when i submit form validation show message. I want to change this message.How can i do this.
所以现在当我提交表单验证显示消息时。我想更改此消息。我该怎么做。
回答by Sohel0415
You may customize the error messages
used by the form request
by overriding the messages()
method. Add custom messages
on your Venue
class as follows-
您可以通过覆盖该方法来自定义 所error messages
使用的。在您的班级上添加自定义如下-form request
messages()
messages
Venue
public static function messages($id = '') {
return [
'name.required' => 'You must enter your name',
'logo.required' => 'You must upload logo',
'key.rules' => 'your messages'
];
And on your controller add messages
as third parameter
like-
在你的控制器上添加messages
第三个像parameter
-
$this->validate($request, Venue::rules(), Venue::messages());
回答by Dunsin Olubobokun
This is the way I go about this and could serve as a guide. From your form, you've got basically 4 input fields and lets assume they are named name, client, logo and venue_type. The function in your controller that validates the form request can be like below:
N.B - you should put -
use Validator;
use Illuminate\Http\Request;- at the top of your class
这就是我处理此事的方式,可以作为指导。从您的表单中,您基本上有 4 个输入字段,假设它们被命名为name、client、logo 和venue_type。控制器中验证表单请求的函数如下所示:
注意 - 你应该放置 -
使用 Validator; 使用 Illuminate\Http\Request; - 在你的班级中名列前茅
public function validateFormRequest($request){ try { //specify your custom message here $messages = [ 'required' => 'The :attribute field is required', 'string' => 'The :attribute must be text format', 'file' => 'The :attribute must be a file', 'mimes' => 'Supported file format for :attribute are :mimes', 'max' => 'The :attribute must have a maximum length of :max', ]; $validator = Validator::make($request->all(), [ 'name' => 'required|string|max:75', 'client' => 'required|string|max:75', 'logo' => 'required|file|mimes:jpeg,png,jpg,gif', 'venue_type' => 'required|string', ], $messages); if($validator->fails()){ // Validation Failed..log errors or Return Errors to view/blade } else{ // Validation passed..Return true or positive info. i.e request can be saved } }catch (Exception $ex){ //Log your errors or return some error message to your view/blade } }
回答by PSA
You can custom the validation message by ,
您可以通过以下方式自定义验证消息,
Go to the resources->lang->en->validation.php
前往 resources->lang->en->validation.php
and here you see,
你看,
'custom' => [
'attribute-name' => [
'rule-name' => 'custom-message',
],
],
Edit those as per your need.
根据您的需要编辑这些。
回答by Etibar
you can add custom errors like this.
您可以添加这样的自定义错误。
$validation->errors()->add('error_input', 'error text');
return redirect()->back()->withInput()->withErrors($validation);
or
或者
return redirect()->back()->withInput()->withErrors(['error_input'=> 'error text');