Forms and success

Well,

This is a newbie, but I really don't know which is the best way. I have a basic CRUD (Create, Retrieve, Update and Delete) in my project and I would like to display some message if it succeeds or fails in a div inside the same page.

So basically, I have a form that has an action set to the same page, and I have a div #statusDiv below the same form that I would like to output as successful using Register.

What's the best way to do this?

  • Set a flag in the controller $this->view->flagStatus = 'message'

    then call it in the view?

Just to make it clearer. This is my code:

//IndexController.php indexAction()

...

//Check if there submitted data
if ($this->getRequest()->isPost()) {
    ...
    $registries->insert($data);
    $this->view->flagStatus = 'message';
}

      

Then my view:

....
<?php if ($this->flagStatus) { ?>   
    <div id="divStatus" class="success span-5" style="display: none;">
        <?php echo $this->flagStatus; ?>
    </div>
<?php } ?>
....

      

+2


a source to share


1 answer


In this situation, since you are redirecting, this this-> view-> flagStatus flag will be lost. You should use the FlashMessenger Action helper instead:

http://framework.zend.com/manual/en/zend.controller.actionhelpers.html

basically you use it the same way as you do now, except that you changed:

$this->view->flagStatus = 'message';

      

to

$this->_helper->flashMessenger->addMessage('message');

      



after that you will need to send the flashMessenger object to the view. You must do this in a location that is executed on every page request so that you can post messages to any page:

$this->view->flashMessenger = $this->_helper->flashMessenger;

      

and then change your view to:

<?php if($this->flashMessenger->hasMessages(): ?> 
    <div id="divStatus" class="success span-5" style="display: none;">
        <?php $messages = $this->flashMessenger->getMessages(); ?>
        <?php foreach($messages as $message): ?>
        <p><?= $message; ?></p>
        <?php endforeach; ?>
    </div>
<?php endif; ?>

      

Hope this helps!

+6


a source







All Articles