Laravel validation - More readable messages - php

Is there a simple way to replace the input name in Laravel's validation messages with for example a label?
Right now message is like:
The usrRoleId field is required.
I know there are two options:
Change input names to more readable.
Write EACH message in the validation request to look like this: User Role is required.
I'm looking for something more automatic - like defining a string once and using it every time such field name is used.

Yes you can and it's actually quite easy. You can specify a custom name for a given attribute in your resources/lang/en/validation.php file under the attributes key:
'attributes' => [
'usrRoleId' => 'User Role'
],
And now anywhere you have usrRoleId for the :attribute inside a validator message, it will be replaced with User Role. So instead of:
The usrRoleId field is required.
You'll get the nice:
The User Role field is required.
If you want to have a whole custom message not only a custom attribute name for when the usrRoleId is required, then you can add an entry for that user the custom key in the same file. So adding this:
'custom' => [
'usrRoleId' => [
'required' => 'User Role is required',
],
],
Will use that custom message but without an :attribute placeholder. If you want to use the placeholder you can leave the attributes mapping I described above:
'custom' => [
'usrRoleId' => [
'required' => ':attribute is required',
],
],
And you'll still get your custom message but with the mapped attribute name:
User Role is required.

Related

Upon validation laravel sends the column name to the user

I have the following validation code in a controller. The problem is that laravel is sending the column name in the validation error , which is most of the time abbreviations. In this case the column name is "ans". Is there a way to show a different name to the user ?
Validation :
$this->validate($request, [
'ans' => 'required|max:5000|min:10'
]);
Error :
The ans field is required.
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.
Yes, you can achieve with the following code.
$this->validate($request, [
'ans' => 'required|max:5000|min:10'
], [
'ans.required' => 'The answer field is required.'
]);
Take a look at the laravel documentation to know more.
You can send custom validation message,define all
your validation message in a variable like this
$messages = [
'ans.required' => 'The answer field is required.'
]
$this->validate($request, [
'ans' => 'required|max:5000|min:10'
], $messages);
Thanks

How to use Not Equal Validation in laravel

Hy! how can i use the not equal in validation
i'm using laravel 5.8. i want to do when i put the (name) something like Admin in input field at the submission of the form it show me error the You can't Use the Name Admin This is all for guest use. when the user is login the he/she can add this name
Laravel offers notIn validation rule for this:
Validator::make($data, [
'name' => [
'required',
Rule::notIn(['Admin']), // You can add more values to the array to black list it.
],
]);
Read more about this here.

Ignore a unique role with custom key - Laravel - Mongodb

In my project with Laravel 5.6 and MongoDB, to validate my inputs in an update method, I use a validator like below,
$validator = Validator::make($request->all(), [
'name' => 'string|max:255',
'phone' => 'string|valid_phone',
'email' => ['string', 'email', 'max:255',
Rule::unique('admins','email')->ignore($id),
],
'password' => 'string|min:6',
'access' => 'numeric',
]);
I want the field to be unique and ignore the same email for the user with special $id.
Everything looks Ok! But when I call my route to update my user and pass the current email of the user as email, It returns a validator error like this,
"email": [
"The email has already been taken."
]
So, the unique validation did not work correctly!
I also have been set the $primaryKey='_id'; in my user model.
What's the problem? Have I missed something?
If you use $primaryKey='_id' in user model you should set second parameter in ignore method. Below is a quote from documentation:
If your table uses a primary key column name other than id, you may
specify the name of the column when calling the ignore method
Rule::unique('admins','email')->ignore($id,'_id')

Laravel use a custom validation message

I have made a custom rule in Laravel 5.5, but also want to get a custom translation with it from the lang validation file. For that I have now done:
'custom' => [
'validate' => [
'correct_password' => 'The :attribute is incorrect.',
],
],
And I put this in the custom rule file:
return trans('validate.correct_password');
What have I done wrong to get the custom message? Because I get now only back the key: validate.correct_password as a message.
If you want to pull a key from the translation file, then you need to pass it a key path in the form of file.key.subkey.subkey.
return trans('validation.custom.validate.correct_password');

Ignore category in update form

Im updating a category name, but i need to be unique and also case is the current record let it update, but it is not working my validation.
Example:
$this->validate($request, array(
'category' => 'required|unique:categories,name,:id|min:2'
));
From unique() rule description:
Sometimes, you may wish to ignore a given ID during the unique check. For example, consider an "update profile" screen that includes the user's name, e-mail address, and location. Of course, you will want to verify that the e-mail address is unique. However, if the user only changes the name field and not the e-mail field, you do not want a validation error to be thrown because the user is already the owner of the e-mail address.
To instruct the validator to ignore the user's ID, we'll use the Rule class to fluently define the rule. In this example, we'll also specify the validation rules as an array instead of using the | character to delimit the rules:
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);

Categories