Ignore a unique role with custom key - Laravel - Mongodb - php

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

Related

Laravel validation throws error on updating same unique value

I have a form which has an input name. I want to update a database table through that form. The only condition is, the name must be unique. So this is what I did for validation in the backend:
$this->validate($request, [
'name' => 'required|unique:myTableName'
]);
It's working as expected. It throws this error "The name has already been taken." when I try to enter a name which already exists in the database. But the problem I have with this is, when I'm updating the same entry from database without any change (I.E. I go to edit form, do nothing, update the form), it shows me the same error. In this case, I don't want the error as I'm updating the same value. How can I achieve this with laravel validation?
You have to use following code
$this->validate($request, [
'name' => ['required', Rule::unique('myTableName')->ignore($yourVariable->id)],
]);
Add header section below code
use Illuminate\Validation\Rule;
More Details here
You need to pass the table id, to ignore that field from validation
'name' => 'required|unique:myTableName,name,' . $table->id,
With custom Rule :
use Illuminate\Validation\Rule;
'name' => ['required', Rule::unique('myTableName', 'name')->ignore($table->id)],

Laravel Validation: soft delete + unique email + update

I'm trying to set up a client registration check and profile edit.
I'm using soft delete
E-mail has to be unique (not counting the delted ones of course)
If profile is edited (id is present) it has to ignore that fields email
So far I have
'email' => [
'required',
'email',
'unique:clients,email,NULL,deleted_at,deleted_at,NULL',
],
Now this works correctly and detects the unique emails that are not deleted, but it gives me errors when I need to edit the profile off the client. I tried adding additional unique settings, but they seem to be getting ignored:
'email' => [
'required',
'email',
'unique:clients,email,NULL,deleted_at,deleted_at,NULL',
Rule::unique('clients')->ignore('id', (int)$request->input('id')) // <- from Laravel docs, but is not working
],
I found solutions with only one of these requirements, but I can't seem to combine them.
Edit: The fix was
'email'=>['required','email',Rule::unique('clients','email')->ignore($request->input('id'))->whereNull('deleted_at')],
try this
$this->validate($request,[
'email'=>['required','email',Rule::unique('clients','email')->ignore($id)->whereNull('deleted_at')]
]);

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),
],
]);

Laravel Illuminate\Validation\Rule not found

I am trying to submit a form and validate the content.
In one of the requests I need to make a special rule.
I followed the documentation and it says to use unique and declare a Rule.
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);
I am trying with the example from the documentation, but all I get it this error:
Class 'Illuminate\Validation\Rule' not found
I declared the line
use Illuminate\Validation\Rule;
In my controller, but the error is still there.
The Rule class in the example you posted is for validate an unique field. For examplo if you have an email you will want to be unique in the table, when you are going to edit the record, at saving, will get a validator error because you are saving the same email it is already in the db.
The example you posted:
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);
is related to this case, editing a record and validating the email (unique in table users). This example avoid to validate the email against the same user-
For using it you have to add the class (not included in the laravel installator). Here you have it.
In your question you say about using the unique rule. The unique rule is for fields that has to be unique in a table, an email, an personal identification (law), and more. Verify if the unique rule is what you need.
You dont have to use the Rule class for this.
Simply achieve the same with following rule:
'email' => 'required|unique:users,email,' . $user->id
The Illuminate\Validation\Rule class has been added on Laravel 5.3+.
I almot sure you have tested with an old laravel version.
See issue (PR) 15809: [5.3] Object based unique and exists rules for validation.
I had that problem on version v5.3. I just updated to the latest patch version (v5.3.31 in my case) and all worked correctly!
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);
You have done everything ok so far until you add the line
"Rule::unique('users')->ignore($user->id)"
The only reason why this is not working is because you are not specifying the column name you want to be unique. Let's assume that, in your database, the column name of the table users that you want to be unique is "email". Then, your code should be as follows:
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users', 'email')->ignore($user->id),
],
]);
Thus,
//Right Way
"Rule::unique('users', 'email')->ignore($user->id)"
//Wrong Way
"Rule::unique('users')->ignore($user->id)"
Why don't you try in you method something like this:
$validator = Validator::make($request->all(), [
'email' => 'required|unique:<table_name>',
]);
if ($validator->fails()) {
...
}
If you column if also named email laravel will know automaticly, else do
required|unique:<table_name>,<column_name>
Here is a link to the documentation validation section: https://laravel.com/docs/5.3/validation

Laravel validation - More readable messages

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.

Categories