Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Laravel 4 Basics Validation & Flash Messages Basic Validation

Nick Basile
Nick Basile
17,942 Points

Laravel Validation Error

Hello! My code looks like this:

public function store(Request $request)
    {
        //define rules
        $rules = array(
            'title' => array('required', 'unique:todo_lists')
        );
        //pass input to validator
        $validator = Validator::make(Input::all(), $rules);
        //test if input is valid
        if($validator->fails()) {
          return redirect()->route('todos.create');  
        }
        $name = $request->get('title');
        $list = new \App\TodoList();
        $list->name = $name;
        $list->save();
        return redirect()->route('todos.index');
    }

and I'm getting the following error:

FatalErrorException in TodoListController.php line 51: Class 'App\Http\Controllers\Validator' not found

I tried adding \App\ to the Validator, and I checked my app.php to see if the aliases was there - it was... Any ideas? Thanks!

Nick Basile
Nick Basile
17,942 Points

Sorry about the formatting - the backticks aren't working for me.

1 Answer

Nick Basile
Nick Basile
17,942 Points

Figured it out. Basically, I couldn't use a double array inside the $rules. So, I changed it to $rules = array( 'title' => 'required', 'unique:todo_lists' );

Also needed to add, use Validator; and use Input; to the top of my TodoListController.php.