92

我正在尝试使用“language > {language} > validation.php”中的验证属性,将 :attribute name (input name) 替换为正确的可读名称(例如:first_name > First name)。使用起来似乎很简单,但验证器没有显示“好名字”。

我有这个:

'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

以及控制器中的验证:

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

$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.'
);

有人可以告诉我我做错了什么。我希望 :attribute 名称被属性数组(语言)中的“好名字”替换。

谢谢!

编辑:

我注意到问题是我从未为我的 Laravel 项目设置默认语言。当我将语言设置为“NL”时,上面的代码有效。但是,当我设置我的语言时,该语言将出现在 url 中。我更喜欢它没有。

所以我的下一个问题是:是否可以从 url 中删除语言,或者设置默认语言,使其不出现在那里?

4

10 回答 10

156

是的,几个月前你所说的“好名字”属性是一个真正的“问题”。希望这个功能现在已经实现并且使用起来非常简单。

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

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

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

    'attributes' => array('first_name' => 'First Name')
    
  2. 本地化 这就是 Taylor Otwell 在这个问题中所说的说:

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

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

    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上有一个非常棒的存储库,其中有很多语言包可供使用。绝对你应该检查一下。

希望这可以帮助。

于 2013-12-17T11:34:02.543 回答
41

这个特定问题的正确答案是转到您的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
),

所以我相信这个数组是专门为自定义这些属性名称而构建的。

于 2016-01-19T15:15:27.710 回答
32

从 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 
于 2016-03-22T14:42:53.950 回答
8

在“属性”数组中,键是输入名称,值是要在消息中显示的字符串。

一个例子,如果你有这样的输入

 <input id="first-name" name="first-name" type="text" value="">

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

 'attributes' => array(
    'first-name' => 'Voornaam'),

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

编辑

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

如果您有这样的代码,请在控制器中解决此问题

return Redirect::route('page')->withErrors(array('register' => $validator));

然后你必须像这样将“注册”键(或你正在使用的任何东西)传递给has()方法

@if($errors->has('register')
.... //code here
@endif

另一种显示错误消息的方法是我更喜欢的以下方法(我使用 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>
于 2013-06-11T22:42:26.453 回答
4

在 Laravel 4.1 中,最简单的方法是转到 lang 文件夹 -> 你的语言(默认 en) -> validation.php。

例如,当您的模型中有此内容时:

'group_id' => 'Integer|required',
'adult_id' => 'Integer|required',

而且您不希望错误是“请输入组 ID”,您可以通过在 validation.php 中添加自定义数组来创建“不错的”验证消息。因此,在我们的示例中,自定义数组如下所示:

'custom' => array(
    'adult_id' => array(
        'required' => 'Please choose some parents!',
    ),
    'group_id' => array(
        'required' => 'Please choose a group or choose temp!',
    ),
),

这也适用于多语言应用程序,您只需要编辑(创建)正确的语言验证文件。

默认语言存储在 app/config/app.php 配置文件中,默认为英文。这可以随时使用该App::setLocale方法进行更改。

可以在此处找到有关错误和语言的更多信息验证本地化

于 2014-02-06T11:58:32.043 回答
4

在 Laravel 7 中。

use Illuminate\Support\Facades\Validator;

然后定义 niceNames

$niceNames = array(
   'name' => 'Name',
);

最后,只需将 $niceNames 放在第四个参数中,如下所示:

$validator = Validator::make($request->all(), $rules, $messages, $niceNames);
于 2020-05-04T17:34:42.407 回答
2

我使用我的自定义语言文件作为“好名字”的输入,如下所示:

$validator = Validator::make(Input::all(), $rules);
$customLanguageFile = strtolower(class_basename(get_class($this)));

// translate attributes
if(Lang::has($customLanguageFile)) {
    $validator->setAttributeNames($customLanguageFile);
}
于 2016-09-23T09:57:34.027 回答
1

: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!',
);
于 2013-06-11T15:17:30.740 回答
1
$customAttributes = [
'email' => 'email address',
];

$validator = Validator::make($input, $rules, $messages, $customAttributes);
于 2020-07-08T22:19:25.290 回答
0

好吧,这是一个相当古老的问题,但我很少应该指出,语言出现在 URL 上的问题可以通过以下方式解决:

  • config/app.php在;处更改语言和 fallback_language
  • 或通过设置 \App::setLocale($lang)

如果需要通过会话保持它,我目前使用“AppServiceProvider”来执行此操作,但是,如果需要通过 URL 更改语言,我认为中间件可能是更好的方法,所以,我在我的提供程序中确实喜欢这样:

    if(!Session::has('locale'))
    {
        $locale = MyLocaleService::whatLocaleShouldIUse();
        Session::put('locale', $locale);
    }

    \App::setLocale(Session::get('locale'));

这样我处理会话并且它不会停留在我的网址上。

为了澄清我目前使用的是 Laravel 5.1+,但不应该是与 4.x 不同的行为;

于 2016-06-10T02:26:09.550 回答