Laravel Validation Attributes "Nice Names"

Laravel, Validation with custom field names

From Laravel Doc..

  1. Custom Error Messages

You may use custom error messages for validation instead of the defaults. There are several ways to specify custom messages. First, you may pass the custom messages as the third argument to the Validator::make method:

$messages = [
'name.required' => 'The "name" field is required!',
'name.unique' => 'The "name" field is should be unique!',
];

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

  1. Specifying Custom Attributes In Language Files

If you would like the :attribute portion of your validation message to be replaced with a custom attribute name, you may specify the custom name in the attributes array of your resources/lang/xx/validation.php language file:

'attributes' => [
'name_field' => 'name',
'number_in_db' => 'number',
],

There are some more options available in the official doc. But I would suggest you to go with the "Specifying Custom Attributes In Language Files" method.

Laravel 5 Form Request Attribute nice names not working with custom validator

If you don't want to use your resources/lang/en/validation.php file for that purpose, you could change your ValidationServiceProvider's boot function to

    public function boot()
{
Validator::resolver(function($translator, $data, $rules, $messages, $attributes)
{
return new ValidatorExtended($translator, $data, $rules, $messages, $attributes);
});
}

Notice that I added the additional $attributes parameter. Then you can set the "nice" names in your FormRequest Classes like you did before.

   public function attributes(){

return [
'title' => 'topic title',
'content' => 'post content'
];

}

How to give custom field name in laravel form validation error message

You can specify custom error message for your field as follows.

$messages = array(
'cat.required' => 'The category field is required.',
);

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

Please see Custom Error Messages section in laravel documentation for more information.

Or you can keep a mapping for your field names like below. And you can set those into you validator. So you can see descriptive name instead of real field name.

$attributeNames = array(
'name' => 'Name',
'cat' => 'Category',
);

$validator = Validator::make ( Input::all (), $rules );
$validator->setAttributeNames($attributeNames);

How to give Laravel 6 validation attributes a custom name for error message?

As it turns out. I needed to edit the 'attributes' array in my resources/lang/xx/validation.php file.

It turns out like this:

'attributes' => [
'name' => 'Naam'
]

Upon validation laravel sends the column name to the user

If you want to customize the attribute name you can do that; you don't have to customize the message to do this. The validate method takes an array of "custom attribute names" to use (4th argument), just like it takes an array of custom messages (3rd argument).

$this->validate(
$request,
['ans' => 'required|max:5000|min:10'], // rules
[], // custom messages
['ans' => 'Answer'] // custom attribute names
);

You should always have the option to specify custom messages and attributes with the validation methods that are available. These custom attribute names get replaced in the messages where :attribute is used.

If you don't do it this way you would have to create 3 custom messages, one for each rule you have defined to use for your field 'ans', since any of those rules could fail and will include the attribute name.



Related Topics



Leave a reply



Submit