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.
Any help would be greatly appreciated.
No comments:
Post a Comment