A
A
Alexander Muxalich2015-05-10 17:58:07
Zend Framework
Alexander Muxalich, 2015-05-10 17:58:07

What is the best way to notify about incorrect work?

Hello, tell me how best to implement the notification of incorrectly entered data. Authorization error and so on. Exception - seems to fit, but everyone advises using it in extreme cases like a database connection error. And every time to issue the ERROR_USER_AUTH variable and throw it along the function stack, it seems to me not true.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Cat Anton, 2015-05-10
@muxalich

There is a formElementErrors view helper for displaying validation error messages .
It is enough to add the necessary validators for the form, but you can, of course, explicitly set the error message using the setMessages() method of the form element:

if ($form->isValid()) {
    $data = $form->getData($form::VALUES_AS_ARRAY);

    $authService = $this->getServiceLocator()->get('AuthenticationService');
    $authService->getAdapter()->setIdentity($data['identity'])->setCredential($data['password']);
    $result = $authService->authenticate();

    if (!$result->isValid()) {
        switch ($result->getCode()) {
            case $result::FAILURE_IDENTITY_NOT_FOUND :
                $form->get('identity')->setMessages(['Неверный логин']);
            break;
            case $result::FAILURE_CREDENTIAL_INVALID :
                $form->get('password')->setMessages(['Неверный пароль']);
            break;
            default :
                $form->get('identity')->setMessages(['Ошибка авторизации']);
            break;
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question