16

I would like to know when a validation failed by using this kind of code writing (I'm using laravel 5.4)

$this->validate($request, [
    'name' => 'required|min:2|max:255'
]);

I know that I can use this:

$validator = Validator::make($request->all(), [
    'name' => 'required|min:2|max:255'
]);

if ($validator->fails()) { //Not okay }

But I would like to keep this way of validating by using $this->validate instead of using the Validator model.

So ... is it possible to use something like:

//This is not working btw

$test = $this->validate($request, [
    'name' => 'required|min:2|max:255'
]);

if( $test )
{ //Ok }
else
{ //Not okay };
2
  • Try using FormRequests. Commented Jan 16, 2018 at 11:51
  • What is not working? Is it the validate method ? Commented Jan 16, 2018 at 13:34

4 Answers 4

32

You can use it like this:

$request->validate($rules);

or

$request->validate([
    'name' => 'required|min:2|max:255'
]);

Then it returns the errors.

$test = $request->validate([
           'name' => 'required|min:2|max:255'
        ]);

and you need to check if there are no errors and then you can do what ever you want.

In your case you need to do it like this:

$validator = Validator::make($request->all(), [
    'name' => 'required|min:2|max:255'
]);

if ($validator->fails()) {
    return view('view_name');
} else {
    return view('view_name');
}
Sign up to request clarification or add additional context in comments.

3 Comments

So it's not possible? I am forced to use the Validator method?
Yes. Not possible... if you want to redirect to some other page or do some custom stuff you need to use Validator::make() because it has that method fails(). Maybe you could create your own fails() method for $request()->validate();
use Illuminate\Contracts\Validation\Validator; don't forget to use this
3
try{

$validateData = Validator::make($request->all(),
[
'name' => 'required|string|min:2|max:255,
]);

if($validateData->fails()){

            return response()->json([
            'status' => false,
            'message' => 'validation error',
            'errors' => $validateData->errors()
        ], 401);

$user = User::create([
'name'=>$request->name
]);

return response()->json([
      'message' => 'User created Successfuly',
      'user' => $user
    ],200);
}

catch(Throwable $th){
         return response()->json([
                    'message' => $th->getMessage()
                ], 500);
            }

2 Comments

The question was already answered, no need to reply 4 years later dude :-)
This helped me thank you!! 🙏 I'll just add that you have to import from use Illuminate\Support\Facades\Validator;
1
use Illuminate\Contracts\Validation\Validator;

then

    $validator =  Validator::make($request->all(), [
        'email' => 'required|email',
        'password' => 'required',
    ]);

    if ($validator->fails()) {
        
    } else {
        # code...
    }

1 Comment

Validator is not imported from use Illuminate\Contracts\Validation\Validator;,rather it's use Illuminate\Support\Facades\Validator;
-1

Request validate will return true or false based on if your validation fails or passes. You can use this to determine what what logic should happen if it returns false.

if($request->validate()){
   // logic for when validation passes
} else {
   // logic for when validation fails
{

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.