4

I've ran into a strange issue regarding validations in Laravel 5.2. I reviewed following questions on StackOverflow, but none of them seems to apply to my case:

Laravel validation not showing errors

Laravel Validation not returning error

The thing is, that I am trying to validate a title field, before persisting the Card object into the database. When I submit the form with an empty title field, as expected, It doesn't pass the validations. However, the $errors array doesn't get populated upon failure of the mentioned validations. Can anybody explain where am I going wrong with this code?

/////////////////////// CONTROLLER /////////////////////
public function create(Request $request)
{
    $this->validate($request, [
        'title' => 'required|min:10'
    ]);

    Card::create($request->all());
    return back();
}
///////////////////////// VIEW /////////////////////////
// Show errors, if any. (never gets triggered)
@if(count($errors))
    <ul>
        @foreach($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
    </ul>
@endif
<form method="POST" action="/cards">
    {{ csrf_field() }}

    <div class="form-group">
        // The textarea does not get populated with the 'old' value as well
        <textarea class="form-control" name="title">{{ old('title') }}</textarea>
    </div>

    <div class="form-group">
        <button class="btn btn-primary" type="submit">Add Card</button>
    </div>
</form>
Community
  • 1
  • 1
Alex Lomia
  • 6,705
  • 12
  • 53
  • 87
  • 2
    Can You attach your `routes.php` file as well as full controller file? Somehow I believe, that some double redirection occurs and your `$errors` does not pass from one redirect to other. Or if You are using restful routes, `create` method is not the place to put your storing function - `store` is the place. – Giedrius Kiršys Apr 08 '16 at 16:59
  • @GiedriusKiršys The problem was indeed in the routes file, because I was including 'web' middleware group twice(as the accepted answer pointed out). Thanks for the advice about restful routes, will keep it in mind. – Alex Lomia Apr 08 '16 at 18:33

3 Answers3

21

If you are running Laravel 5.2.27 and up, you no longer need to use the web middleware group. In fact, you shouldn't add it to your routes because it's now automatically applied by default.

If you open up your app/Http/RouteServiceProvider.php file, you will see this bit of code:

protected function mapWebRoutes(Router $router)
{
    $router->group([
        'namespace' => $this->namespace, 'middleware' => 'web',
    ], function ($router) {
        require app_path('Http/routes.php');
    });
}

Source: https://github.com/laravel/laravel/blob/master/app/Providers/RouteServiceProvider.php#L53

As you can see, it's automatically applying the web middleware for you. If you try to apply it again (more than once) in your routes file, you'll run into weird problems like what you are currently facing.

In order to find out the version of Laravel that you are running, run this command: php artisan --version

Thomas Kim
  • 15,326
  • 2
  • 52
  • 42
0

I guess you have to set the if clause to @if(count($errors) > 0)

Norgul
  • 4,613
  • 13
  • 61
  • 144
0

In your controller, try adding a $validator->fails() statement, and using ->withErrors() to return any errors to your template.

public function create(Request $request)
{
    $validator = Validator::make($request->all(), [
        'title' => 'required|min:10'
    ]);

    if ($validator->fails()) {
        return back()->withErrors($validator);
    }

    Card::create($request->all());
    return back();
}
Islam Al-Rayan
  • 396
  • 2
  • 7
Alessandro Bassi
  • 427
  • 5
  • 14