简体   繁体   中英

twig - impossible to access a key (“message”) on a NULL variable

Ok, First off, I know there have been similar questions asked about this subject

Such as: Impossible to access an attribute Error

However, they don't exactly answer the question I have. I am setting up a login system for an application I am writing and have been getting an error when trying to display the login authentication error messages.

I have the following code in my template:

{% if error %}
    {% block message %}{{ error.message }}{% endblock %}
{% endif %}

This is what I have in the controller that is calling the template:

public function loginAction()
{
    $request = $this->getRequest();
    $session = $request->getSession();

    // get the login error if there is one
    if($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)){
        $error = $request->attributes->get(
            SecurityContext::AUTHENTICATION_ERROR
        );
    } else {
        $error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
        $session->remove(SecurityContext::AUTHENTICATION_ERROR);
    }

    return $this->render(
            'SaveSecurityBundle:Security:login.html.twig',
            array(                    
                'last_username' => $session->get(SecurityContext::LAST_USERNAME),
                'error'   => $error,
            )
        );
}

This should be fairly simple, however I keep getting the following message when I try to load the login form:

Impossible to access a key ("message") on a NULL variable ("") in SaveSecurityBundle:Security:login.html.twig at line 5

I tried doing a dump of the error variable and got the following (I'm only including the lines up to the piece that I actually need, the actually dump is a couple thousand lines):

object(Symfony\Component\Security\Core\Exception\BadCredentialsException)#49 (8) {
    ["token":"Symfony\Component\Security\Core\Exception\AuthenticationException":private]=>
object(Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken)#50 
["message":protected]=>
   string(15) "Bad credentials"

So the message is there, but for some reason when it is supposed to be passing the error object, it is passing a null reference.

I am at a complete loss as to how to fix this, my only solution so far has been to remove the error printout all together, which removes the ability to inform the user of why they have not been logged in.

The problem is that you're rendering the error message inside a Twig block and you can't put a block into an if block — it renders regardless of the condition.

One of the solutions would be to make the block wrap the if statement:

{% block message %}
    {% if error %}{{ error.message }}{% endif %}
{% endblock %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM