Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 427
hatemjapo Avatar asked Nov 14 '16 10:11

hatemjapo


People also ask

How to use validationexception in Laravel?

Create a new validation exception from a plain array of messages. Get all of the validation error messages. Set the HTTP status code to be used for the response. Set the error bag on the exception.

How can I check email is valid in Laravel?

You can check if an email is valid or not in Laravel using the validate method by passing in the validation rules. You can validate your email addresses using one or more of the validation rules we have discussed.

How do I create a custom validation rule in Laravel?

Custom Validation Rule Using Closures $validator = Validator::make($request->post(),[ 'birth_year'=>[ 'required', function($attribute, $value, $fail){ if($value >= 1990 && $value <= date('Y')){ $fail("The :attribute must be between 1990 to ". date('Y').". "); } } ] ]);

What is the method used to configure validation rules in form request?

Laravel Form Request class comes with two default methods auth() and rules() . You can perform any authorization logic in auth() method whether the current user is allowed to request or not. And in rules() method you can write all your validation rule.


2 Answers

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.

like image 194
Programador Adagal Avatar answered Sep 21 '22 21:09

Programador Adagal


You dont have to use the Rule class for this.

Simply achieve the same with following rule:

'email' => 'required|unique:users,email,' . $user->id
like image 36
henrik Avatar answered Sep 25 '22 21:09

henrik