Zend flash messenger

I have a form, and the submit button checks if it is true or false.

If true, redirect to another page.

If it is false, stay on the same page and print an error message.

The error message is printed using the flash messenger. But in some cases, it does not print in the first attempt, when submit is false, it always prints in the second click.

Did I do something wrong? And is there also a way to make a difference in the name of a flash messenger? Because on other pages that have a flash messenger, print an error message when you refresh the page.

Here is the code:

if(isset($_POST['submit'])) {
    // code to inputfields

    if(true) {
        //redirect to some page
    } else {
        // print the flash error on the same page
        $this->_helper->flashMessenger->addMessage(" This email is already taken");
        $this->view->messages = $this->_helper->flashMessenger->getMessages();
    }
}

HTML:

<center>
            <div style="color:red">
            <?php if (count($this->messages)) : ?>
                <?php foreach ($this->messages as $message) : ?>
                <div id="field_name">
                <strong style="text-transform:capitalize;">Email </strong>
                    - <?php echo $this->escape($message); ?>
                </div>
                <?php endforeach; ?>
            <?php endif; ?>
            </div>
        </center>
+5
source share
1 answer

flashmessenger - , , , , . "".

, - :

public function init()
{
    //This will catch any messege set in any action in this controller and send
    //it to the view on the next request.
    if ($this->_helper->FlashMessenger->hasMessages()) {
        $this->view->messages = $this->_helper->FlashMessenger->getMessages();
    }
}

public function someAction()
{
    if(isset($_POST['submit'])) {
    // code to inputfields
        if(true) {
        //redirect to some page
        } else {
            // print the flash error on the same page
            $this->_helper->flashMessenger->addMessage(" This email is already taken");
            //will redirect back to original url. May help, may not
            $this->_redirect($this->getRequest()->getRequestUri());
        }
    }
}

, , , .

public function updatetrackAction()
    {
        //get the page number
        $session = new Zend_Session_Namespace('page');
        $id = $this->getRequest()->getParam('id');
        //get the entity object
        $model = new Music_Model_Mapper_Track();
        $track = $model->findById($id);
        //get the form
        $form = new Admin_Form_Track();
        $form->setAction('/admin/music/updatetrack/');
        //test for 'post' 'valid' and update info
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $data = $form->getValues();

                $newTrack = new Music_Model_Track($data);

                $update = $model->saveTrack($newTrack);
                //add message
                $this->message->addMessage("Update of track '$update->title' complete!");
                //redirects back to the same page number the request came from
                $this->getHelper('Redirector')->gotoSimple('update', null, null, array('page' => $session->page));
            }
        } else {
            //if not post display current information
            //populate() only accepts an array - no objects -
            $form->populate($track->toArray());
            $this->view->form = $form;
        }
    }
+8

All Articles