Ran
Ran

Reputation: 4157

Zend - Hide Form and Show Message

I have a zend form with a few fields.

When the form validated I would like to hide the form and show a success message.

There are so many ways to do it - redirect to another controller action, render another view script, flag in the view helper/session and more.

Is there any best practice for this?

Thanks.

Upvotes: 2

Views: 836

Answers (3)

markus
markus

Reputation: 40685

I think quite often people use the solution posted in two other answers which checks if the form has been posted and is valid and then passes a message to the view.

I think the approach is ok but not optimal. It ties messages closely into the workflow and creates the need for code repetition. This is not necessary. IMO messaging should be taken out of the workflow and handled with a reusable component. As it happens, Zend Framework provides us with then necessary tool, the flashMessenger.

So, what I started doing is sending a message to the flashMessenger, whenever I need one, it will be shown after the next roundtrip automatically, independent of the form action and takes away the need for additional markup and if statements.

Examples and resources concerning the flashMessenger:

FlashMessenger works best with the additional view helper found via these links!

Upvotes: 1

JellyBelly
JellyBelly

Reputation: 2431

I think this:

//into controller
$form = new Form_Foo();
if ($this->getRequest()->isPost() && $form->isValid($_POST)) {
    $this->view->message = "Post Successful!!!";
} else {
    $this->view->form = $form;
}

and

//into view
<?= $this->message ?>
<?= $this->form ?>

Upvotes: 1

tasmaniski
tasmaniski

Reputation: 4898

This is my way:

In controller,when the form was submitted as successfull,I set

$this->view->success_msg = "Success.";

and in view file I just ask:

<?php
$success_msg = $this->success_msg;
if(isset($success_msg)){
    echo $success_msg;
}else{
    echo $form;
}
?>

Upvotes: 1

Related Questions