有没有办法在 Laravel 5 验证规则中为输入名称设置别名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28976775/
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
Is there a way to alias input names in the rules for Laravel 5 validation?
提问by prograhammer
I like the feature in Laravel 5 that let's me throw my validation logic into one of Laravel 5's Form Requests and then Laravel will automatically validate it just beforemy controller's method is ran. However, one thing that is missing is an easy way to "alias" an input name.
我喜欢 Laravel 5 中的功能,它让我将验证逻辑放入 Laravel 5 的表单请求之一,然后Laravel 将在我的控制器方法运行之前自动验证它。但是,缺少的一件事是“别名”输入名称的简单方法。
For example (for simplicity sake), say I have a login form with an input field called "username" but it actually accepts an email address. The label on my form says Email. If there's an error, the error will say something like "Username is required". This is confusing since I'm labeling the field as Emailon my form.
例如(为简单起见),假设我有一个带有名为“用户名”的输入字段的登录表单,但它实际上接受一个电子邮件地址。我的表格上的标签是Email。如果出现错误,错误会显示“需要用户名”之类的内容。这很令人困惑,因为我在表单上将该字段标记为电子邮件。
What's a good solution for aliasing inputs when using Laravel's validator?
使用 Laravel 的验证器时,输入别名的好的解决方案是什么?
So far I've come up with two ideas:
到目前为止,我提出了两个想法:
Solution 1:Use Laravel's custom error messages for each rule
解决方案 1:对每个规则使用 Laravel 的自定义错误消息
<?php namespace App\Http\Requests;
use App\Http\Requests\Request;
class AuthLoginRequest extends Request {
public function rules()
{
return [
'username' => 'required|email',
'password' => 'required'
];
}
// This is redundant. And may have other pitfalls?
public function messages()
{
return [
'username.required' => 'email is required.',
'username.email' => 'email is invalid.'
];
}
...
}
Solution 2:Use my own custom form class to handle changing the input names to their label/alias (usernamebecomes emailfor validation purposes), running validation, then changing them back.
解决方案 2:使用我自己的自定义表单类来处理将输入名称更改为其标签/别名(用户名变为电子邮件用于验证目的),运行验证,然后将它们更改回来。
回答by GeraldBiggs
`
`
use App\Http\Requests\Request;
class AuthLoginRequest extends Request {
public function rules()
{
return [
'username' => 'required|email',
'password' => 'required'
];
}
public function messages()
{
return [
'username.required' => ':attribute is required',
'username.email' => ':attribute is invalid'
];
}
public function attributes()
{
return[
'username' => 'email', //This will replace any instance of 'username' in validation messages with 'email'
//'anyinput' => 'Nice Name',
];
}
}`
}`
Updated
更新
I believe anywhere there's an :attribute
in your messages will be either default to the name set in your form, or overridden by the method attributes()
above. (on :attribute
http://laravel.com/docs/5.0/validation#custom-error-messages)
我相信:attribute
您的消息中的任何地方都将默认为您表单中设置的名称,或者被上述方法覆盖attributes()
。(在:attribute
http://laravel.com/docs/5.0/validation#custom-error-messages 上)
Footnote: I have not explained how I'm doing a dynamic array for my form, as I'm iterating over each form input in a way which may be clumsy. I did notice that I'm using the following line for my form request. I thought I'd mention that here in case:
脚注:我没有解释我如何为我的表单做一个动态数组,因为我正在以一种可能很笨拙的方式迭代每个表单输入。我确实注意到我在表单请求中使用了以下行。我想我会在这里提到以防万一:
use Illuminate\Foundation\Http\FormRequest;
Let me know if this is more helpful!
让我知道这是否更有帮助!
回答by Carter
You can customise your validation input names in file resources/lang/en/validation.php, assuming you are using Laravel 5 and using English as locale language by default.
您可以在文件 resources/lang/en/validation.php 中自定义验证输入名称,假设您使用 Laravel 5 并默认使用英语作为区域设置语言。
You can find an array called 'custom', simply customise your input names and validation messages there.
您可以找到一个名为“custom”的数组,只需在那里自定义您的输入名称和验证消息。
For example:
例如:
'custom' => array(
'username' => array(
'email' => 'Username is actually an email.'
),
)
where 'username' is your input name, 'email' is the name of built-in rule, and 'Username is actually an email' is whatever you want to tell your user.
其中“用户名”是您的输入名称,“电子邮件”是内置规则的名称,“用户名实际上是电子邮件”是您想告诉用户的任何内容。
Hope this helps!
希望这可以帮助!
回答by Toni
Open the file resources/lang/en/validation.php, where en is the default language of the app. thars if you are using English. At the bottom of the file, update the attributes array as the following:
打开文件resources/lang/en/validation.php,其中en是应用的默认语言。如果您使用的是英语,那就太棒了。在文件底部,更新属性数组如下:
'attributes' => array( 'password' => 'Password', 'email' => 'Email Address', ),
Add other attribute names and the corresponding error label. It will do the trick you wanted.
添加其他属性名称和相应的错误标签。它会做你想要的把戏。
回答by Rodrigo Pauletti
Use this:
用这个:
$validator = Validator::make($data, [
'username' => 'required|string', // Your field validation
])
->setAttributeNames(
['username' => '"Username"'], // Your field name and alias
);