Laravelvalidation属性“好名字”

我试图在“语言> {language}> validation.php”中使用validation属性来replace:属性名称(input名称)以获取正确的名称(例如:名字>名字)。 使用起来似乎很简单,但validation器不显示“好名字”。

我有这个:

'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:

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

有人能告诉我我做错了吗? 我想要的属性名称被replace为属性数组(语言)中的“好名字”。

谢谢!

编辑:

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

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

是啊,你称之为“好名字”的属性是几个月前的一个真正的“问题”。 希望这个function现在已经实现,而且非常简单易用。

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

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

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

     'attributes' => array('first_name' => 'First Name') 
  2. 本地这是泰勒奥特韦尔在这个问题上谈论的时候,他说:

    您现在可以在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上有一个非常棒的回购,有很多的语言包可以准备好了。 当然,你应该检查出来。

希望这可以帮助。

这个问题的正确答案是去你的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 ), 

所以我相信这个数组是专门为这些属性名称定制的。

既然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 

在“attributes”数组中,键是input名称,值是要在消息中显示的string。

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

  <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进行devise,但是当然你可以用自己的devise改变它们)

  @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> 

:属性只能使用属性名称(first_name在你的情况下),而不是好名字。

但你可以定义自定义消息为每个属性+validation通过定义这样的消息:

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

迟到的答案,但可能有助于某人

在Laravel 4.1中,简单的方法是进入lang文件夹 – >你的语言(default en) – > validation.php

然后例如,当你在你的模型中

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

并且不要因为错误而please enter a group id

你可以通过在自定义数组中添加validation.php来创build“nice”validation消息,所以在我们的例子中,自定义数组看起来像这样

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

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

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

有关错误和语言的更多信息可以在这里findvalidation和本地化

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

 $validator = Validator::make(Input::all(), $rules); $customLanguageFile = strtolower(class_basename(get_class($this))); // translate attributes if(Lang::has($customLanguageFile)) { $validator->setAttributeNames($customLanguageFile); } 

那么,这是一个相当古老的问题,但我应该指出的是,出现在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')); 

这样我处理会话,它不会粘在我的url。

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