Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vercoutere's avatar

Testing for validation errors

I have a controller that validates some input:

public function postLogin(Request $request) {

    $this->validate($request, [

        'email' => 'required|email',
        'password' => 'required',
    ]);
}

In my view I have the following piece of code to display these errors:

@if (count($errors) > 0)
    <div class="row">
        <div class=" alert alert-danger col-sm-offset-2 col-sm-10">
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    </div>
@endif

and I have a test that verifies this to be happening:

/** @test */
public function it_validates_login() {

    $this->visit('/login')
        ->press('Login')
        ->see('The password field is required')
        ->see('The email field is required');
}

This is all very straight forward and was working as expected. The test was green and everyone was happy.

However, since updating my code to use the "web" middleware group that is provided in Laravel 5.2 on all routes the test is failing. When I try this myself in the browser it still works (as it should) though, so this has to be something specific to do with testing. PHPUnit tells me Failed asserting that the page contains the HTML [The password field is required]. Please check the content above., and indeed, the errors are not in the HTML code PHPUnit is comparing to.

I have tried disabling the ShareErrorsFromSession middleware in the web middleware group, but that actually gave me more errors. The more I started messing with this the less sense it made.

I'm now under the impression that these errors shouldn't even work without using the web middleware group, but they do. And when I enable the web middleware group but delete all middleware classes that are bound to it in kernel.php it doesn't work and throws an error saying Undefined variable: errors. What's the difference between having no classes in there or just not using it?

Anyway, the issue being: Validation works like a charm, but the logical testing procedures provided by Laravel don't seem to work for verifiying this behavior?!

0 likes
1 reply
vercoutere's avatar
vercoutere
OP
Best Answer
Level 2

Classic; Been searching for this for over an hour, and 2 minutes after giving in and asking for help I find what's going on...

In the default RouteServiceProvider all routes in routes.php are already being group in the web middleware group. In other words, my routes were passing through the same middleware twice because I was also declaring a route group with this middleware in routes.php.

Obviously, the fact that these errors already worked without grouping my routes under this middleware wrapper kinda gave it away. I'll blame this one on being mega-tired! :----)

Hope this post will at least help someone else!

Please or to participate in this conversation.