- 2008-04-18 (Fri) 7:00
- Zend_Form
intro
This article shows Multi Page Form that extends Zend_Form.
1.Create Form
Create the file
library/My/Form/MultiPage.php with the following content.
<?php require_once 'Zend/Form.php'; require_once 'Zend/Session/Namespace.php'; class My_Form_MultiPage extends Zend_Form { const BUTTON_SUBMIT = 'submit'; const BUTTON_NEXT = 'next'; const BUTTON_BACK = 'back'; const BUTTON_CANCEL = 'cancel'; protected $_name = null; protected $_names = array(); protected $_session = null; protected $_canceled = false; protected $_multi_page = true; protected $_submit_decorators = array( 'ViewHelper', array('HtmlTag', array('tag' => 'dt', 'class' => 'submit')) ); public function __construct($name, $multi_page = true) { parent::__construct(); $this->_name = $name; $this->_multi_page = $multi_page; $this->_session = new Zend_Session_Namespace($name); if (!isset($this->_session->current)) { $this->_session->current = 0; $this->_session->confirmed = false; } } protected function _loadForm($name, $form) { if($this->_session->values[$name]) { $form->setDefaults($this->_session->values[$name]); } } protected function _storeForm($name, $form) { if ($this->_multi_page) { if ($form instanceof Zend_Form_SubForm) { $values = $form->getValues(); $this->_session->values[$name] = $values[$name]; } else { $this->_session->values[$name] = $form->getValues(); } } else { $this->_session->values = $this->getValues(); } } public function addForm($name, $form) { $this->_names[] = $name; $this->_loadForm($name, $form); $this->addSubForm($form, $name); } public function clear() { unset($this->_session->values); unset($this->_session->current); unset($this->_session->confirmed); } public function hasForm() { return count($this->_names); } public function getCurrentFormName() { return $this->_names[$this->_session->current]; } public function back() { if ($this->_session->current > 0) $this->_session->current--; } public function isValid($data) { if ($this->_multi_page) { $name = $this->getCurrentFormName(); $form = $this->getCurrentForm(); $result = $form && $form->isValid($data); if ($result) { $this->_session->current++; $this->_storeForm($name, $form); } } else { $result = parent::isValid($data); if ($result) { $this->_session->current = count($this->_names); $this->_storeForm($this->getName(), $this); } } return $result; } public function setDefaults($values) { foreach ($values as $table_name => $table_values) { if (array_search($table_name, $this->_names) !== FALSE) { $subform = $this->getSubForm($table_name); $subform->setDefaults($table_values); $this->_storeForm($table_name, $subform); } } } public function isFirstForm() { return $this->_session->current == 0; } public function isConfirm() { return count($this->_names) <= $this->_session->current; } public function getCurrentForm() { if ($this->_multi_page && isset($this->_names[$this->_session->current])) { $subform = $this->getSubForm($this->_names[$this->_session->current]); return $subform; } else if (!$this->_multi_page) { return $this; } return null; } public function prepareForm($action, $submit_label) { if ($this->isConfirm()) { $form = new Zend_Form(); } else { $form = $this->getCurrentForm(); if ($form instanceof Zend_Form_SubForm) { $subform = $form; $form = new Zend_Form(); $form->addSubForm($subform, $this->getCurrentFormName()); } } $form->setAction($action) ->setMethod('post'); if (!$this->isFirstForm()) { $back = $form->createElement('submit', self::BUTTON_BACK); $back->setLabel(self::BUTTON_BACK); $back->setDecorators($this->_submit_decorators); $form->addElement($back); } if ($this->isConfirm()) { $next = $form->createElement('submit', $submit_label); $next->setLabel($submit_label); $next->setDecorators($this->_submit_decorators); } else { $next = $form->createElement('submit', self::BUTTON_NEXT); $next->setLabel(self::BUTTON_NEXT); $next->setDecorators($this->_submit_decorators); } $form->addElement($next); $cancel = $form->createElement('submit', self::BUTTON_CANCEL); $cancel->setLabel(self::BUTTON_CANCEL); $cancel->setDecorators($this->_submit_decorators); $form->addElement($cancel); return $form; } }
Next
Next article will show how to use this form class.
History
| Date | Content |
|---|---|
| 2008/4/18 | Published |
| 2008/4/18 | The decorators of submit buttons was changed. |
| 2008/4/26 | It is modified to use SubForm. |
- Newer: Using Multi Page Form that extends Zend_Form
- Older: Implementing Add/Update/Delete/View Screens
Comments:0
Trackbacks:0
- Trackback URL for this entry
- http://www.oplabo.com/article/26/trackback
- Listed below are links to weblogs that reference
- Creating Multi Page Form that extends Zend_Form from Open Programming Laboratory