Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel validation attributes "nice names"

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".

I have this:

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

For showing the errors:

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

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

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).

Thanks!

EDIT:

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?

like image 679
Hak Avatar asked Jun 11 '13 14:06

Hak


People also ask

How can I validate my name in laravel?

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". And the validation in the controller: $validation = Validator::make($input, $rules, $messages);

What is unique in laravel validation?

To add the unique validation in laravel migration, you have to tell the laravel that field will be unique. And you can do this by the unique() method. This case prompt, when you know that field will be unique in the database and no user will have the same data twice.


1 Answers

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. 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') 
  2. Locally This is what Taylor Otwell was talking about in the issue when he says:

    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; } 

    So, to use this way see the following straightforward example:

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

Resources

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

Hope this helps.

like image 84
Javier Cadiz Avatar answered Sep 27 '22 22:09

Javier Cadiz