3 months ago by Yoshi Mannaert

Relates to the lesson Authentication with Slim 3

I'm following along the Slim 3 authentication course and have done exactly what is shown in the videos.

I'm now on the part where you show error messages on the form after validation, which works, but when you load the signup page for the first time it gives the following error:

Notice: Undefined index: errors in .../app/Middleware/ValidationErrorsMiddleware.php on line 9 The code in that file is this:

<?php

namespace App\Middleware;

class ValidationErrorsMiddleware extends Middleware
{
    public function __invoke($request, $response, $next)
    {
        $this->container->view->getEnvironment()->addGlobal('errors', $_SESSION['errors']);
        unset($_SESSION['errors']);

        $response = $next($request, $response);
        return $response;
    }
}

I'm assuming the error happens because it wants to show the error messages while there are no errors (since the form hasn't been filled in yet).

I've downloaded the code from the video and checked everything but it all seems the same to my code so I can't figure out why this error is happening

3 months ago by Lee Weaver

It the form has a say

<input type="text" name="Full Name" value="">

if your puting from into a database

and

name="FullName" does not match in th database

Undefined index

Check for that

Or

if you hav not completed the for variables proeprly on the form

It will give undefined index as well

The code can be fine

But if he database doesn ,match or you havent completed the form properly youll get that undfined index hope hat helps

But remebvr your useing class so

that there is refering to the form

$this->container->view->getEnvironment()->addGlobal('errors', $_SESSION['errors']); unset($_SESSION['errors']);

Remebr that so evebn fi the forms the problem or the databse it will go to where the source is is which is hthis

$this->container->view->getEnvironment()->addGlobal('errors', $_SESSION['errors']); unset($_SESSION['errors']);

Not your form

3 months ago by Lee Weaver

Also if the codes fine you should nt o be getting undefined in desx if the database is fine the codes obvioulsy beedn calledd for some unknow reason thats why you got the eror the chances thats the databse

3 months ago by CHRIS CANHAM

Best answer

@Mosh I had the same problem last year. I found a work a round. whether this is bad practice I don't know but it is simple a case of wrapping a function in an if statement:

 class ValidationErrorsMiddleware extends Middleware
{
    public function __invoke($request, $response, $next)
    {   
        if (isset($_SESSION['errors'])) {
            $this->container->view->getEnvironment()->addGlobal('errors', $_SESSION['errors']);
            unset($_SESSION['errors']);
        }
        
        $response = $next($request, $response);
        return $response;
    }
}

your going to find the same thing happen for anything else set into sessions such as 'old' and 'user'

I hope this helps??

3 months ago by Yoshi Mannaert

@C4NH4M From the research I've done you're answer is the same I've come across so I'm assuming that's the correct answer. Your answer does offer the only solution that works so far so I'm assuming that's the way to do it. Though I haven't had that error with the 'old' session which is why I don't really understand the problem itself. But I'm glad I'm at least not the only one who has come across this problem!

3 months ago by CHRIS CANHAM

@Mosh I think maybe that our development environments play a roll in what we see. I am using Xampp on a 64 bit Windows 7 machine, It's getting a bit old now lol :)

Sign in or create an account to reply to this topic.