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: f
The correct answer to this particular problem would be to go to your app/lang folder and edit the validation.php file at the bottom of the file there is an array called 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
),
So I believe this array was built to customise specifically these attribute names.
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:
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')
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.
I use my custom language files as Input for the "nice names" like this:
$validator = Validator::make(Input::all(), $rules);
$customLanguageFile = strtolower(class_basename(get_class($this)));
// translate attributes
if(Lang::has($customLanguageFile)) {
$validator->setAttributeNames($customLanguageFile);
}
The :attribute can only use the attribute name (first_name in your case), not nice names.
But you can define custom messages for each attribute+validation by definine messages like this:
$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!',
);
Since Laravel 5.2 you could...
public function validForm(\Illuminate\Http\Request $request)
{
$rules = [
'first_name' => 'max:130'
];
$niceNames = [
'first_name' => 'First Name'
];
$this->validate($request, $rules, [], $niceNames);
// correct validation
In Laravel 7.
use Illuminate\Support\Facades\Validator;
Then define niceNames
$niceNames = array(
'name' => 'Name',
);
And the last, just put $niceNames in fourth parameter, like this:
$validator = Validator::make($request->all(), $rules, $messages, $niceNames);