Laravel Illuminate\Validation\Rule not found
Asked Answered
T

6

10

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.

Truditrudie answered 14/11, 2016 at 10:36 Comment(3)
I have the same issue. It seems Laravel don't include the Rule class. I am looking how to add it.Marilou
Did u solve it?Nunn
Which version of Laravel are you using?Tiruchirapalli
M
6

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.

Marilou answered 22/2, 2017 at 9:46 Comment(0)
C
3

You dont have to use the Rule class for this.

Simply achieve the same with following rule:

'email' => 'required|unique:users,email,' . $user->id
Celle answered 14/11, 2016 at 11:4 Comment(7)
i have tried this. But my column name ist not id. I can write .',columnname' but for some reason this is not working. Its not ignoring this entry..Truditrudie
Whats the column name?Celle
'league_id' is the column nameTruditrudie
Then this should work: 'email' => 'required|unique:users,email,' . $user->id . ',league_id' <-if this doen't work, which laravel version are you running?Celle
i have tried this before and its not working. I have laravel 5.3Truditrudie
Well then, your validation rule is correct, but fails due to something else. use dd() to try to find what is causing the validator to not work. For example: $validator = Validator::make(.... if($validator->fails()) dd($validator->failed) and so on...Celle
Let us continue this discussion in chat.Truditrudie
S
0

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.

Stoneham answered 16/7, 2018 at 20:34 Comment(0)
B
0

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!

Bravado answered 14/3, 2019 at 1:35 Comment(0)
K
0
    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)"
Konya answered 9/1, 2020 at 16:49 Comment(0)
T
-1

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

Tolman answered 14/11, 2016 at 11:8 Comment(1)
Useless answer. Read the documentation before posting. What he is asking is about the documentation.Marilou

© 2022 - 2024 — McMap. All rights reserved.