简体   繁体   中英

Zend Framework addErrorMessage issue

I am not sure if I am following it properly. I am trying to set a custom error message to a Zend form so that the view can display that error message.

Here is my Conroller

public function indexAction()
{
    $request = $this -> getRequest();
    $frmRegister = new Application_Form_RegisterForm();

    if ($request -> isPost())
    {
        if ($frmRegister -> isValid($request -> getPost()))
        {
            // Everything appears valid.
            // Build the array
            $data = array(
                'firstname' => $request -> getPost('firstname'),
                'lastname' => $request -> getPost('lastname'),
                'email' => $request -> getPost('email'),
                'password' => hash('SHA512', $request -> getPost('password'))
            );

            $result = $this -> userModel -> registerUser($data);

// $result['statusmsg'] returns an error message "The user already exists" and $result['status'] = 0 

            if ($result['status'] == 0)
            {
                $frmRegister ->  addErrorMessage($result['statusmsg']) ;
                $this -> view -> form = $frmRegister;
            }
        }
        else
        {
            // Errors! We show the same page
            $this -> view -> form = $frmRegister;
        }
    }
    else
    {
        $this -> view -> form = $frmRegister;
    }
}

View page

<?php
    if(isset($this->form))
    $errors = $this->form->getMessages();
    if(count($errors) > 0 )
    {
        echo '<div class="alert alert-danger">';
        foreach($errors as $field => $arrErrors)
        {
            foreach($arrErrors as $error )
            {
                echo '<li>' . $this->form->getElement($field)->getLabel() . ' - ' . $error . '</li>';
            }
        }
        echo '</div>';
    }
       if(isset($this->loginError))
           echo '<div class="alert alert-danger">'. $this->loginError . '</div>';
    ?>

The issue is that the error is not displaying. Where am I wrong ?

Have you var_dump()ed your $this->form->getMessages() ?

It looks like that your errors are not field related - therefore not displayed in your loop. BTW your FormErrors decorator should render your field related errors next to the form fields by default.

After some searching, I was able to get the result by tweaking the code

if ($result['status'] == 0)
{
    // Errors! We show the same page
    $this -> view -> registerError = $result['statusmsg'];
    $frmRegister ->setErrors(array($result['statusmsg']));
    $frmRegister->markAsError() ;
    $this -> view -> form = $frmRegister;
}

And the view is changed to

if(isset($this->form))
    $errors = $this->form->getMessages();
    if(count($errors) > 0 )
    {
        echo '<div class="alert alert-danger">';
        foreach($errors as $field => $arrErrors)
        {
            if(is_array($arrErrors))
            foreach($arrErrors as $error )
            {
                echo '<li>' . $this->form->getElement($field)->getLabel() . ' - ' . $error . '</li>';
            }
            else
            {
                echo $arrErrors;
            }
        }
        echo '</div>';
    }

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