Laravel验证属性"好名字"

Hak*_*Hak 79 php multilingual laravel laravel-3

我正在尝试使用"language> {language}> validation.php"中的验证属性来替换正确读取名称的:属性名称(输入名称)(例如:first_name>名字).使用起来似乎很简单,但验证器没有显示"好名字".

我有这个:

'attributes' => array(
    'first_name' => 'voornaam'
  , 'first name' => 'voornaam'
  , 'firstname'  => 'voornaam'
);
Run Code Online (Sandbox Code Playgroud)

用于显示错误:

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

并在控制器中进行验证:

$validation = Validator::make($input, $rules, $messages);
Run Code Online (Sandbox Code Playgroud)

$ messages数组:

$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.'
);
Run Code Online (Sandbox Code Playgroud)

有人能告诉我我做错了什么.我希望:属性名称被属性数组(语言)中的"漂亮名称"替换.

谢谢!

编辑:

我注意到问题是我从未为我的Laravel项目设置默认语言.当我将语言设置为'NL'时,上面的代码可以正常工作.但是,当我设置我的语言时,语言将出现在网址中.我更喜欢它没有.

所以我的下一个问题是:是否可以从网址中删除该语言,或设置默认语言,以便它不会出现在那里?

Jav*_*diz 137

是的,你所谓的"好名字"属性是几个月前真正的"问题".希望这个功能现在已经实现,并且非常简单易用.

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

  1. 全球可能更广泛.这种方法在这里得到了很好的解释,但基本上你需要编辑application/language/XX/validation.php验证文件,其中XX是你将用于验证的语言.

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

    'attributes' => array('first_name' => 'First Name')
    
    Run Code Online (Sandbox Code Playgroud)
  2. 本地这就是Taylor Otwell在这个问题上所说的话,他说:

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

    这是完全有效的,如果你检查源代码,你会看到

    public function setAttributeNames(array $attributes)
    {
        $this->customAttributes = $attributes;
    
        return $this;
    }
    
    Run Code Online (Sandbox Code Playgroud)

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

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

资源

Github上有一个非常棒的回购,有很多语言包准备好了.你肯定应该检查一下.

希望这可以帮助.

  • 全局解决方案存在问题:在一种形式中,我可能有'name'=>'客户名',而在另一种形式中,它可能是''name'=>'卖方名称'.我更喜欢每个模块都有我的翻译,所以我也可以将这些值重用为表单字段的标签.因此,我投票支持本地解决方案,但是如何从我的语言文件中检索$ niceNames数组?如何在不创建新的Validator实例但是使用控制器中可用的$ this-> validate()方法的情况下调用`setAttributeNames`? (2认同)
  • 为了回应这个评论并完成@javiercadiz的解决方案,自从Laravel 5.1你可以在`$ this-> validate(Request $ request,$ rules,$ messages,$ customAttributes)中使用第四个参数来获得这些好名字.在[Laravel 5.1 API - 验证请求]中查看(http://laravel.com/api/5.1/Illuminate/Foundation/Validation/ValidatesRequests.html#method_validate) (2认同)

Log*_*gus 26

这个特定问题的正确答案是转到你的app/lang文件夹并编辑文件底部的validation.php文件,有一个名为attributes的数组:

/*
|--------------------------------------------------------------------------
| Custom Validation Attributes
|--------------------------------------------------------------------------
|
| The following language lines are used to swap attribute place-holders
| with something more reader friendly such as E-Mail Address instead
| of "email". This simply helps us make messages a little cleaner.
|
*/

'attributes' => array(
    'username' => 'The name of the user',
    'image_id' => 'The related image' // if it's a relation
),
Run Code Online (Sandbox Code Playgroud)

所以我相信这个数组是为了专门定制这些属性名而构建的.

  • 这是用西班牙语''attributes'=>''name'=>'nombre','mail'=>'correo','password'=>'contraseña','password2'=>'confirmmación decontraseña',],` (2认同)

小智 22

从Laravel 5.2开始,你可以......

public function validForm(\Illuminate\Http\Request $request)
{
    $rules = [
        'first_name' => 'max:130'
    ];  
    $niceNames = [
        'first_name' => 'First Name'
    ]; 
    $this->validate($request, $rules, [], $niceNames);

    // correct validation 
Run Code Online (Sandbox Code Playgroud)


Alt*_*rim 8

在"attributes"数组中,键是输入名称,值是要在消息中显示的字符串.

举例来说,如果你有这样的输入

 <input id="first-name" name="first-name" type="text" value="">
Run Code Online (Sandbox Code Playgroud)

数组(在validation.php文件中)应该是

 'attributes' => array(
    'first-name' => 'Voornaam'),
Run Code Online (Sandbox Code Playgroud)

我尝试了同样的事情,效果很好.希望这可以帮助.

编辑

另外我注意到你没有传递参数,$errors->has()所以也许这就是问题所在.

如果您有这样的代码,请在控制器中修复此签出

return Redirect::route('page')->withErrors(array('register' => $validator));
Run Code Online (Sandbox Code Playgroud)

那么你必须传递给has()方法"注册"键(或者你正在使用的任何东西),就像这样

@if($errors->has('register')
.... //code here
@endif
Run Code Online (Sandbox Code Playgroud)

另一种显示错误消息的方法是我更喜欢的一种方式(我使用Twitter Bootstrap进行设计,当然你可以用你自己的设计改变它们)

 @if (isset($errors) and count($errors->all()) > 0)
 <div class="alert alert-error">
    <h4 class="alert-heading">Problem!</h4>
     <ul>
        @foreach ($errors->all('<li>:message</li>') as $message)
         {{ $message }}
       @endforeach
    </ul>
</div>
Run Code Online (Sandbox Code Playgroud)


Ale*_*ult 0

:attribute 只能使用属性名称(在您的例子中是first_name),而不是好听的名称。

但是您可以通过定义消息为每个属性+验证定义自定义消息,如下所示:

$messages = array(
  'first_name_required' => 'Please supply your first name',
  'last_name_required' => 'Please supply your last name',
  'email_required' => 'We need to know your e-mail address!',
  'email_email' => 'Invalid e-mail address!',
);
Run Code Online (Sandbox Code Playgroud)