1

I have simple login in zf2. I want to show error message error to user when username/password didn't match.

In view/login.php:

 if (isset($error_msg)) { echo $error_msg; } 

In Controller I have:

$msg = array('error_msg' => 'Invalid Username or Password'); return $this->redirect()->toRoute('login', $msg); 

Here error_msg cannot passed to view, what are wrong with this?

Furthermore I also try

$model = new ViewModel(array( 'error_msg' => 'Wrong username or password', )); $model->setTemplate('login/index'); return $model; 

But here link didn't go to login/index. but instead go to login/process. Process is the action in which login is processed.

Help me friends please. I want to pass error message from controller to view . So how should I do it.

2
  • You don't need a redirect for this. Just use the current view to render the error. Commented Feb 18, 2017 at 20:48
  • So, how? code please Commented Feb 19, 2017 at 1:29

1 Answer 1

2

Just put your login process code into your login index action, there's no need to have two actions for this. When you have processed your login and determined that it has failed simply pass your error message to your view model and display it in your view. This can be done in several ways.

$viewModel = new ViewModel(); $viewModel->error_msg = 'Wrong username or password'; return $viewModel; return new ViewModel(array( 'error_msg' => 'Wrong username or password', )); 

or simply

return array( 'error_msg' => 'Wrong username or password', ); 
Sign up to request clarification or add additional context in comments.

1 Comment

Ayaz, if the problem is resolved with this answer please mark it as correct.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.