Laravel验证属性“好名称" [英] Laravel validation attributes "nice names"

查看:87
本文介绍了Laravel验证属性“好名称"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用语言> {语言}> validation.php"中的验证属性,以:attribute名称(输入名称)替换为正确的名称(例如:first_name>名字).使用起来似乎很简单,但是验证器不会显示好名字".

I'm trying to use the validation attributes in "language > {language} > validation.php", to replace the :attribute name (input name) for a proper to read name (example: first_name > First name) . It seems very simple to use, but the validator doesn't show the "nice names".

我有这个:

'attributes' => array(
    'first_name' => 'voornaam'
  , 'first name' => 'voornaam'
  , 'firstname'  => 'voornaam'
);

显示错误:

@if($errors->has())
  <ul>
  @foreach ($errors->all() as $error)
    <li class="help-inline errorColor">{{ $error }}</li>
  @endforeach
  </ul>
@endif

并在控制器中进行验证:

And the validation in the controller:

$validation = Validator::make($input, $rules, $messages);

$ messages数组:

The $messages array:

$messages = array(
    'required' => ':attribute is verplicht.'
  , 'email'    => ':attribute is geen geldig e-mail adres.'
  , 'min'      => ':attribute moet minimaal :min karakters bevatten.'
  , 'numeric'  => ':attribute mag alleen cijfers bevatten.'
  , 'url'      => ':attribute moet een valide url zijn.'
  , 'unique'   => ':attribute moet uniek zijn.'
  , 'max'      => ':attribute mag maximaal :max zijn.'
  , 'mimes'    => ':attribute moet een :mimes bestand zijn.'
  , 'numeric'  => ':attribute is geen geldig getal.'
  , 'size'     => ':attribute is te groot of bevat te veel karakters.'
);

有人可以告诉我我在做什么错.我希望将:attribute名称替换为属性数组(语言)中的好名称".

Can someone tell me what i'm doing wrong. I want the :attribute name to be replaced by the "nice name" in the attributes array (language).

谢谢!

我注意到问题是我从未为Laravel项目设置默认语言.当我将语言设置为"NL"时,上面的代码有效.但是,当我设置语言时,该语言将出现在url中.我更喜欢不这样做.

I noticed that the problem is I never set a default language for my Laravel projects. When I set the language to 'NL' the code above works. But, when I set my language, the language will appear in the url. And I prefer it doesn't.

所以我的下一个问题:是否可以从网址中删除该语言,或设置默认语言以使其不在此处显示?

So my next question: Is it possible to remove the language from the url, or set the default language so it just doesn't appear there?

推荐答案

是的,几个月前,您所说的好名字"属性是真正的问题". 希望此功能现在已已实现并且非常易于使用.

Yeahh, the "nice name" attributes as you called it was a real "issue" a few month ago. Hopefully this feature is now implemented and is very simply to use.

为简单起见,我将拆分两个选项来解决此问题:

For simplicity i will split the two options to tackle this problem:

  1. 全球可能范围更广. 此处对此方法进行了很好的解释,但基本上您需要进行编辑 application/language/XX/validation.php 验证文件,其中XX是用于验证的语言.

  1. Global Probably the more widespread. This approach is very well explained here but basically you need to edit the application/language/XX/validation.php validation file where XX is the language you will use for the validation.

在底部,您将看到一个属性数组;那将是你的好名字"属性数组.按照您的示例,最终结果将是这样.

At the bottom you will see an attribute array; that will be your "nice name" attributes array. Following your example the final result will be something like this.

'attributes' => array('first_name' => 'First Name')

  • 本地,这就是Taylor Otwell在问题,当他说:

  • Locally This is what Taylor Otwell was talking about in the issue when he says:

    您现在可以在Validator实例上调用setAttributeNames.

    You may call setAttributeNames on a Validator instance now.

    那是完全有效的,如果您查看源代码您将会看到

    That's perfectly valid and if you check the source code you will see

    public function setAttributeNames(array $attributes)
    {
        $this->customAttributes = $attributes;
    
        return $this;
    }
    

    因此,要使用这种方式,请参见以下简单的示例:

    $niceNames = array(
        'first_name' => 'First Name'
    );
    
    $validator = Validator::make(Input::all(), $rules);
    $validator->setAttributeNames($niceNames); 
    

  • 资源

    Github 上有一个很棒的仓库,其中有很多语言包可供使用.绝对应该把它签出来.

    There is a really awesome repo on Github that have a lot of languages packages ready to go. Definitely you should check it out.

    希望这会有所帮助.

    这篇关于Laravel验证属性“好名称"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆