intro
This article shows how to use the multi page form created in the previous article. You can download the source code of this article from here.
1.Create bootstrap.php and config.ini
Create the file
Please modify
Create the file
Please modify
In this article, we will use the following sql.
application/bootstrap.php
with the following content.Please modify
yourBaseUrl
properly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php set_include_path('../library' . PATH_SEPARATOR . get_include_path()); define('APP_BASE', '../application'); define('CONFIG_PATH', APP_BASE . '/config.ini'); require_once 'Zend/Controller/Front.php'; require_once 'Zend/Layout.php'; require_once 'Zend/Db.php'; require_once 'Zend/Config/Ini.php'; require_once 'Zend/Db/Table/Abstract.php'; $layout = Zend_Layout::startMvc(); //$layout->getView()->baseUrl = '/yourBaseUrl'; $config = new Zend_Config_Ini(CONFIG_PATH, 'staging'); $params = $config->database->params->toArray(); $params['options'][Zend_Db::CASE_FOLDING] = Zend_Db::CASE_LOWER; $dbAdapter = Zend_Db::factory($config->database->adapter, $params); Zend_Db_Table_Abstract::setDefaultAdapter($dbAdapter); Zend_Controller_Front::run(APP_BASE . '/controllers'); |
Create the file
application/config.ini
with the following content.Please modify
database.params.*
properly.
1 2 3 4 5 6 |
[staging] database.adapter = pdo_mysql database.params.host = localhost database.params.username = db_user database.params.password = db_password database.params.dbname = db_name |
In this article, we will use the following sql.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
CREATE TABLE users ( id integer auto_increment NOT NULL PRIMARY KEY, username varchar(20) NOT NULL, password varchar(20) NOT NULL ); CREATE TABLE profiles ( id integer auto_increment NOT NULL PRIMARY KEY, user_id integer NOT NULL, first_name varchar(100) NOT NULL, last_name varchar(100) NOT NULL, email varchar(200) NOT NULL, url varchar(200) NOT NULL ); |
2.Create Models
Create the file
Create the file
application/models/Users.php
with the following content.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?php require_once 'Zend/Db/Table/Abstract.php'; require_once 'Zend/Form.php'; class Users extends Zend_Db_Table_Abstract { protected $_name = 'users'; protected $_dependentTables = array('Profiles'); static public function getForm() { $form = new Zend_Form(); $username = $form->createElement('text', 'username'); $username->setLabel('Account Name') ->setRequired(true) ->addFilter('stringTrim') ->addValidator('stringLength', false, array(1,20)); $password = $form->createElement('password', 'password'); $password->setLabel('Password') ->setRequired(true) ->addFilter('stringTrim') ->addValidator('stringLength', false, array(1,20)); $form->addElements(array( $username, $password )); return $form; } } |
Create the file
application/models/Profiles.php
with the following content.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<?php require_once 'Zend/Db/Table/Abstract.php'; require_once 'Zend/Form.php'; class Profiles extends Zend_Db_Table_Abstract { protected $_name = 'profiles'; protected $_referenceMap = array( 'Account' => array( 'columns' => 'user_id', 'refTableClass' => 'Users', 'refColumns' => 'id' ) ); static public function getForm() { $form = new Zend_Form(); $first_name = $form->createElement('text', 'first_name'); $first_name->setLabel('First Name') ->setRequired(true) ->addFilter('stringTrim') ->addValidator('stringLength', false, array(1,100)); $last_name = $form->createElement('text', 'last_name'); $last_name->setLabel('Last Name') ->setRequired(true) ->addFilter('stringTrim') ->addValidator('stringLength', false, array(1,100)); $email = $form->createElement('text', 'email'); $email->setLabel('E-Mail') ->setRequired(true) ->addFilter('stringTrim') ->addValidator('emailAddress', false) ->addValidator('stringLength', false, array(1,200)); $url = $form->createElement('text', 'url'); $url->setLabel('URL') ->setRequired(false) ->addFilter('stringTrim') ->addValidator('stringLength', false, array(1,200)); $form->addElements(array( $first_name, $last_name, $email, $url )); return $form; } } |
3.Create Controller
Create the file
application/constorllers/ComputerController.php
with the following content.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
<?php require_once 'Zend/Controller/Action.php'; require_once APP_BASE . '/models/Users.php'; require_once APP_BASE . '/models/Profiles.php'; require_once 'My/Form/MultiPage.php'; class IndexController extends Zend_Controller_Action { protected $_user_id = 1; public function preDispatch() { $this->view->baseUrl = $this->_request->getBaseUrl(); } public function getForm() { $form = new My_Form_MultiPage('UserProfileForm'); $form->addForm('Users', Users::getForm()); $form->addForm('Profiles', Profiles::getForm()); return $form; } public function indexAction() { $this->_forward('list'); } public function listAction() { $users = new Users(); $userlist = $users->fetchAll(); $this->view->userlist = $userlist; } public function viewAction() { $users = new Users(); if (isset($_GET['id']) && intval($_GET['id'])) { $user = $users->find(intval($_GET['id']))->current(); if ($user) { $profile = $user->findProfiles()->current(); if ($profile) { $this->view->profile = $profile->toArray(); } $this->view->user = $user->toArray(); } } } public function addAction() { $action = $this->_request->getBaseUrl() . '/index/add'; $form = $this->getForm(); if ($this->getRequest()->isPost()) { if (isset($_POST['next']) && $form->isValid($_POST)) { if ($form->isConfirm()) { // confirm values $values = $form->getValues(); $this->view->user = $values['Users']; $this->view->profile = $values['Profiles']; $this->view->form = $form->prepareForm($action, 'add'); return $this->render('confirm'); } } else if (isset($_POST['add'])) { // save & finish $values = $form->getValues(); $userValues = $values['Users']; $profileValues = $values['Profiles']; $form->clear(); $users = new Users(); $user = $users->createRow($userValues); $user_id = $user->save(); $profiles = new Profiles(); $profileValues['user_id'] = $user_id; $profile = $profiles->createRow($profileValues); $profile->save(); return $this->render('finish'); } else if (isset($_POST['back'])) { $form->back(); } else if (isset($_POST['cancel'])) { $form->clear(); return $this->_forward('list'); } } $this->view->form = $form->prepareForm($action, 'next'); $this->view->title = $form->getCurrentFormName(); } } |
4.Create View
Create the file
Create the file
Create the file
Create the file
Create the file
application/views/scripts/computer/list.phtml
with the following content.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<h1><?= $this->translate('User List') ?></h1> <a href="<?=$this->url(array('action'=>'add'))?>"> <?= $this->translate('Add User') ?></a> <table> <tr> <th><?= $this->translate('username') ?></th> <th><?= $this->translate('action') ?></th> </tr> <?php foreach($this->userlist as $user) : ?> <tr> <td><?= $this->escape($user->username) ?></td> <td><a href="<?=$this->url(array('action'=>'view')) . '?id=' . $user->id?>"> <?= $this->translate('View') ?></a></td> </tr> <?php endforeach; ?> </table> |
Create the file
application/views/scripts/computer/view.phtml
with the following content.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<h1><?= $this->translate('User Detail') ?></h1> <a href="<?=$this->url(array('action'=>'list'))?>"> <?= $this->translate('Back') ?></a> <h2><?= $this->translate('User Account') ?></h2> <?php if (!$this->user) : ?> <span><?= $this->translate('No User Account Found') ?></span> <?php else : ?> <dl> <?php foreach($this->user as $key => $value) : ?> <dt><?= $this->translate($key) ?></dt> <dd><?= $this->escape($value) ?></dd> <?php endforeach; ?> </dl> <?php endif; ?> <h2><?= $this->translate('User Profile') ?></h2> <?php if (!$this->profile) : ?> <span><?= $this->translate('No Profile Found') ?></span> <?php else : ?> <dl> <?php foreach($this->profile as $key => $value) : ?> <dt><?= $this->translate($key) ?></dt> <dd><?= $this->escape($value) ?></dd> <?php endforeach; ?> </dl> <?php endif; ?> |
Create the file
application/views/scripts/computer/add.phtml
with the following content.
1 2 |
<h1><?= $this->translate($this->title)?></h1> <?= $this->form ?> |
Create the file
application/views/scripts/computer/confirm.phtml
with the following content.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<h1><?= $this->translate('Add User') ?></h1> <h2><?= $this->translate('User Account') ?></h2> <?php if (!$this->user) : ?> <span><?= $this->translate('No User Account Found') ?></span> <?php else : ?> <dl> <?php foreach($this->user as $key => $value) : ?> <dt><?= $this->translate($key) ?></dt> <dd><?= $this->escape($value) ?></dd> <?php endforeach; ?> </dl> <?php endif; ?> <h2><?= $this->translate('User Profile') ?></h2> <?php if (!$this->profile) : ?> <span><?= $this->translate('No Profile Found') ?></span> <?php else : ?> <dl> <?php foreach($this->profile as $key => $value) : ?> <dt><?= $this->translate($key) ?></dt> <dd><?= $this->escape($value) ?></dd> <?php endforeach; ?> </dl> <?php endif; ?> <?= $this->form?> |
Create the file
application/views/scripts/computer/finish.phtml
with the following content.
1 2 3 |
<h1><?=$this->translate('User Added')?></h1> <a href="<?=$this->url(array('action'=>'list'))?>"> <?= $this->translate('Back') ?></a> |
5.Check
Access the web server and check the screen flow.
History
Date | Content |
---|---|
2008/4/19 | Published |
2008/4/26 | The controller was modified for modification of My_Form_MultiPage. Views were modified to use URL helper. |
I am having these errors when I run you sample:
Strict Standards: Declaration of Mylib_Form_MultiPage::setDefaults() should be compatible with that of Zend_Form::setDefaults() in C:\Inetpub\wwwroot\library\Mylib\Form\MultiPage.php on line 202
Strict Standards: Declaration of Mylib_Controller_MultiPage::_executeFind() should be compatible with that of Mylib_Controller_Simple::_executeFind() in C:\Inetpub\wwwroot\library\Mylib\Controller\MultiPage.php on line 6
Notice: Undefined index: Profiles in C:\Inetpub\wwwroot\library\Mylib\Form\MultiPage.php on line 37
I just want this same functionality on the demo page. Maybe I downloaded the wrong codes.
Any assistance would be appreciated.
Thanks
I’m sorry, but I created the program with the following php configuration.
error_reporting = E_ALL & ~E_NOTICE
If you can change your php.ini, please change it.