Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1

(^230) CHAPTER 15 ■ INTRODUCTION TO THE ZEND FRAMEWORK
//If the validators passed this will be true
if ($zfi->isValid()) {
//Fetch the data from zfi directly and create an array for Zend_Db
$clean = array();
$clean['name'] = $zfi->name;
//Create an instance of the customers table and insert the $clean row
$customers = new Customers();
$customers->insert($clean);
//Redirect to the display page after adding
$this->getHelper('redirector')->goto('index');
} else {
//The form didn't validate, get the messages from ZFI
foreach($zfi->getMessages() as $field=>$messages) {
//Put each ZFI message into the FlashMessenger so it shows on the form
foreach($messages as $message) {
$this->getHelper('FlashMessenger')
->addMessage($field. ' : '. $message);
}
}
//Redirect back to the input form, but with messages
$this->getHelper('redirector')->goto('add');
}
}
//Not a post request, check for flash messages and expose to the view
if($this->getHelper('FlashMessenger')->hasMessages()) {
$this->view->messages = $this->getHelper('FlashMessenger')->getMessages();
}
//The view renderer will now automatically render the add.phtml template
}
You will also need to modify your add.phtml template. Add the code shown in Listing 15-17 to
the top of the file.
McArthur_819-9C15.fm Page 230 Thursday, February 28, 2008 7:44 AM

Free download pdf