php 如何在 Laravel 表单验证错误消息中给出自定义字段名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25187846/
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 give custom field name in laravel form validation error message
提问by JOE
I was trying form validation in laravel. I have a input text field in my form called 'Category' and i'm given the field name as 'cat' as short.
我在 Laravel 中尝试表单验证。我的表单中有一个名为“Category”的输入文本字段,我的字段名称为“cat”。
And i defined the validation rules like this.
我定义了这样的验证规则。
public static $rules=array(
"name"=>"required|min:3",
"cat"=>"required"
);
When the validation fails i'm getting error message like this
当验证失败时,我收到这样的错误消息
The name field is required.
The cat field is required.
But i want to display it as "The category field is required" instead of 'cat'.
How can i change 'cat' to 'Category' in error message ?.
但我想将其显示为“类别字段是必需的”而不是“猫”。
如何在错误消息中将“cat”更改为“Category”?
回答by uiroshan
You can specify custom error message for your field as follows.
您可以为您的字段指定自定义错误消息,如下所示。
$messages = array(
'cat.required' => 'The category field is required.',
);
$validator = Validator::make($input, $rules, $messages);
Please see Custom Error Messages section in laravel documentationfor more information.
有关更多信息,请参阅laravel 文档中的自定义错误消息部分。
Or you can keep a mapping for your field names like below. And you can set those into you validator. So you can see descriptive name instead of real field name.
或者您可以为您的字段名称保留映射,如下所示。你可以将这些设置到你的验证器中。因此,您可以看到描述性名称而不是真实的字段名称。
$attributeNames = array(
'name' => 'Name',
'cat' => 'Category',
);
$validator = Validator::make ( Input::all (), $rules );
$validator->setAttributeNames($attributeNames);
回答by GeraldBiggs
I'm using this to deal with dynamic row addition in forms. This answer is provided in context of managing larger forms. This answer is for Laravel 5
我正在使用它来处理表单中的动态行添加。此答案是在管理较大表单的上下文中提供的。这个答案适用于Laravel 5
Form request
表单请求
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Response;
class SomeThingFormRequest extends Request {
public function authorize()
{
//be nice
}
public function rules()
{
//my loop building an array of rules ex: $rules['thing.attribute.'.$x]
}
public function attributes()
{
return [
'thing.attribute'.$x => 'Nice Name',
];
}
Hope this help steer you in the right direction if you are using Laravel 5. I'm currently working on testing it out. The documentation seems to be amiss regarding this potential?
如果您正在使用 Laravel 5,希望这有助于引导您朝着正确的方向前进。我目前正在对其进行测试。关于这种潜力的文件似乎有误?
I did some digging in the framework (Illuminate\Foundation\Http\FormRequest.php) and (Illuminate\Validation\Validator.php) for the clues.
我在框架 (Illuminate\Foundation\Http\FormRequest.php) 和 (Illuminate\Validation\Validator.php) 中挖掘了一些线索。
回答by hamid Reza Kamali
you can customize every message ,also change the attribute fields name in validation.php (resources/lang/en/). for set the attribute in validation.php
您可以自定义每条消息,还可以更改validation.php (resources/lang/en/) 中的属性字段名称。用于在validation.php 中设置属性
'attributes' => [
'name' => 'Name',
'cat' => 'Category',
'field_name'=>'your attribute'
],
回答by Hitender
Simply got to resources/lang/en/validation.php
只需 resources/lang/en/validation.php
There is a blank array named attributes
.
有一个名为 的空白数组attributes
。
add your attribute name here like 'cat'=>'category'
在此处添加您的属性名称,例如 'cat'=>'category'
now all validation messages show category instead of cat.
现在所有验证消息都显示类别而不是猫。
回答by Paul Dela Vega
Here is an Alternative $this->validate(request(), [rules], [custom messages], [Custom attribute name]);
这是一个替代 $this->validate(request(), [rules], [custom messages], [Custom attribute name]);
$this->validate(request(), [
'fname' => "required|alpha_dash|max:20",
'lname' => "required|alpha_dash|max:30",
'extensionName' => "required|alpha_dash|max:20",
'specialization' => "max:100",
'subSpecialization' => "max:100"
], [],
[
'fname' => 'First Name',
'lname' => 'Last Name',
'extensionName' => 'Extension Name',
'specialization'=> 'Specialization',
'subSpecialization'=> 'Sub Specialization'
]);
回答by Calin Blaga
Here's an alternative:
这是一个替代方案:
$data = ['name' => e(Input::get('name')), 'cat' => e(Input::get('cat'))];
$rules = ['name' => 'required|min:3', 'cat' => 'required'];
// pass an item from lang array if you need to
$nice_input_names = ['cat' => trans('lang_file.a_name_for_the_category_input')];
$validator = Validator::make($data, $rules, $messages, $nice_input_names);