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

Delimiting multiple validations as elements of an array isn't working w/ latest stable version of Laravel (4.2).

The following—as shown in the first video about validation in the Laravel course—does not work when it should according to the course to prevent a blank:

$rules = [ 'title' => [ 'required', 'unique:todo_lists,name' ] ]; 
$validator = Validator::make(Input::all(), $rules); 
if($validator->fails()){
  // Redirect to todos.create with validation method
}


// Note: The old array syntax does *not* make a difference, for those unfamiliar with this array literal introduced in PHP some time ago.

My form field for title is as follows:

{{ Form::text( 'title', null, [ 'placeholder' => 'List the name of this new todo list.' ] ) }}

Only after adding the else clause to surround the previous logic to create a new todos list I was able to get the functionality to work.

Being familiar with Laravel in the past (3), the pipe version of writing validations did not work for me, either. That prompted me, seeing no extension from the framework, to then conclude this was a logical error.

Is it safe to assume, Hampton Paulk should look into revising this part of the video to avoid potential confusion by other Treehouse users?

5 Answers

I'll also add that the array form of setting multiple validators for a single field no longer works; required & unique did not work until I used the pipe syntax.

Hey Kevin,

I've tried out your code above and it's working for me without an additional else statement.

Just note that 'title' later changes to 'name' to match the database column.

<?php

$rules = ['name' => [ 'required', 'unique:todo_lists' ] ];
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::route('todos.create')->withErrors($validator)->withInput();
} 

?>

What does the piped syntax look like? I may be looking at the wrong section but "Using Arrays To Specify Rules" http://laravel.com/docs/validation mentions the use of both pipes and arrays for creating validations for a single field...

I've trialed replacing the above $rules with what I think the pipe syntax should look like:

<?php

$rules = ['name' => 'required|unique:todo_lists' ] ;

?>

My outcome is:

Multiple rules on a single field using an array = successful *

Multiple rules on a single field using pipe syntax (if my syntax is correct!) = successful *

*Without additional else statement

Tom Cawthorn: It doesn't work for me w/ the latest stable version of Laravel.

The name of the column doesn't make much of a difference being more of a logical problem. Even changed later on did not fix the problem at all.

Perhaps Hampton Paulk may know something about this I'm not seeing.

Hey Kevin,

Nightmare!

I'm on Laravel Framework version 4.2.7 via Homestead.

In your screenshot that you posted you will for sure need to return line 44 to see any messages, but your syntax looks fine. Can you post your current code in a gist?

Hi, Hampton Paulk:

That was commented out during debugging, sorry for the confusion.

That said, this is what I had at the end of the class that still led to the creation of blank entries in my table without adding explicitly an else clause (which is also strange to me because of the explicit return in the fail clause:

    public function store()
    {
    // define rules
    $rules = [ 'name' =>  'required|unique:todo_lists'  ];
    // pass intput to validator
    $validator = Validator::make(Input::all(), $rules);
    // test if input is fails
    if ($validator->fails()){
      return Redirect::route('todos.create')->withErrors($validator)->withInput();
    } else {
      $name = Input::get('name');
      $list = new TodoList();
      $list->name = $name;
      $list->save();
      return Redirect::route('todos.index')->withMessage('List was created!');
    }
  }