Zend flash messenger
Asked Answered
M

1

5

I have a form and the submit button check if true or false.

If it's true redirect to another page.

If it's false stay on the same page and print the error message.

The error message is print out with a flash messenger. But, in some case it doesn't print in the first try when submit is false, it always print out on the second click.

Did I did something wrong? And also, is there a way to set difference flash messenger name? Because, on my others pages that have flash messenger, print out the error when page is refreshed.

Here's 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>
Micrometer answered 1/2, 2013 at 17:42 Comment(0)
W
9

flashmessenger is really designed to be used on a redirect of some kind, so any message you see probably comes from the prior action's execution. Your current code would not flash any message during the first 'post'.

you may have some luck if you try something like:

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());
        }
    }
}

Here's an action I coded that demonstrates what you seem to be attempting.

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;
        }
    }
Weathers answered 2/2, 2013 at 7:59 Comment(1)
In ZF3, this is $route = $this->getEvent()->getRouteMatch(); return $this->redirect()->toRoute($route->getMatchedRouteName(), $route->getParams());Mcgarry

© 2022 - 2024 — McMap. All rights reserved.