mjonat's avatar

Validation problems

I am trying to implement validation to my app. Mostly its going well...until it goes right? haha...I shall explain...

So I have this:

$rules = [
    'profile_pic' => 'required|mimes:jpg,jpeg,png,bmp', //insert max filesize here aswell
 ];
$validation = $this->validate($request, $rules);
if($validation->fails()){
    return redirect()->to('/profile/'.$id.'/edit');
}

and then on the front:

@if(count($errors))
    <ul>
    @foreach($errors->all() as $error)
        <li class="text-danger">
            {{$error}}
        </li>
    @endforeach
    </ul>
@endif

Which works fine and dandy for out putting errors but then when I actually do what I am checking for correctly and upload an image with the right file size and file extension it gives the error:

Call to a member function fails() on a non-object (null)

How can that be when it works and it is an object but only if something is wrong?

0 likes
7 replies
d3xt3r's avatar

$this->validate($request, $rules);is a method which takes care of validating, redirecting etc, does not return anything, hence the error. You don't need to specifically check if it fails.

If you want to do it yourself. then

$validator = Validator::make($request->all(), $rules);
if($validator->fails()){
    return redirect()->to('/profile/'.$id.'/edit');
}

1 like
Jaytee's avatar

Like d3xt3r said,

$this->validate(); // global helper method which handles validation (succeeds or fails etc) and automatically applies the errors to the $errors variable
Validator::make(); // Facade, handle it how you want it to be handled.
mjonat's avatar

Yeah I found some documentation on this and tried implementing...although not exactly as you have done but i just gave it a try and it gave the same results...I should have mentioned what I have and is semi working is:

$validation = $this->validate($request, $rules);
if($validation->fails()){
    return redirect()->to('/profile/'.$id.'/edit');
}
return view('auth.profile',compact('user'));

If it failed the if statement works and the fail() function works as it redirects there and before i implemented this it did return to the correct view as is stated in the last line but now it errors out saying fails() isnt even a thing?

When I implement as you suggested I just get a white screen no matter if the input is correct or not so I dont know whats happening there...

mjonat's avatar

@Jaytee - so I'm guessing why the suggested Validator::make(); doesnt work for me and just gives me a white page is because its not putting it into $errors anymore?

Jaytee's avatar
Jaytee
Best Answer
Level 39

Try this:

// define your $rules here

$validator = Validator::make($request->all(), $rules);

        if ($validator->fails()) {
            return redirect($putyoururlhere)
                        ->withErrors($validator)
                        ->withInput();
        }

mjonat's avatar

This just returns a HTTP ERROR 500 :(

mjonat's avatar

Ok! so it turns out I was just being an idiot and didnt call it at the top:

use Validator;

My bad! All working nicely now :)

Please or to participate in this conversation.