Re: HTML_Quickform_Controller Jump issue.
| From: | Ian P. Christian | Date: | Fri, 04 Jun 2004 12:13:54 +0000 |
| Subject: | Re: HTML_Quickform_Controller Jump issue. | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-12900@lists.php.net to get a copy of this message | ||
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Friday 04 June 2004 11:18, Ian P. Christian wrote:
> I'm using the function below as the action handler for Page0 in my
> multipage wizard, however I can only seem to get jumps to page0 or page1
> working, jumping to any other page results in page1 (the next page) being
> shown.
Whilst what I am attaching isnt' a complete form (when you get to the end, it
loops back to the beginning!) it does show a solution to my problem. For the
purpose of the archives, I have included it inline in this message. I hope
this if of help to someone.
<?php
require_once 'HTML/QuickForm.php';
require_once 'Var_Dump.php';
require_once 'DB.php';
require_once 'HTML/QuickForm.php';
require_once 'HTML/QuickForm/Controller.php';
require_once 'HTML/QuickForm/Action/Next.php';
require_once 'HTML/QuickForm/Action/Back.php';
require_once 'HTML/QuickForm/Action/Jump.php';
require_once 'HTML/QuickForm/Action/Display.php';
session_start();
class DefaultActionNext extends HTML_QuickForm_Action_Next
{
function perform(&$page, $actionName)
{
// save the form values and validation status to the session
$page->isFormBuilt() or $page->buildForm();
$pageName = $page->getAttribute('id');
$data =& $page->controller->container();
$data['values'][$pageName] = $page->exportValues();
$data['valid'][$pageName] = $page->validate();
// Modal form and page is invalid: don't go further
if (!$data['valid'][$pageName]) {
return $page->handle('display');
}
// More pages?
if (null !== ($nextName = $page->controller->getNextName($pageName))) {
$next =& $page->controller->getPage($nextName);
$next->handle('jump');
// Consider this a 'finish' button, if there is no explicit one
} if($page->controller->isModal()) {
if ($page->controller->isValid()) {
$page->handle('process');
} else {
// this should redirect to the first invalid page
$page->handle('jump');
}
}
}
}
class Page0ActionNext extends HTML_QuickForm_Action_Next
{
function perform(&$page, $actionName)
{
// save the form values and validation status to the session
$page->isFormBuilt() or $page->buildForm();
$pageName = $page->getAttribute('id');
$data =& $page->controller->container();
$data['values'][$pageName] = $page->exportValues();
$data['valid'][$pageName] = $page->validate();
// Modal form and page is invalid: don't go further
if ($page->controller->isModal() && !$data['valid'][$pageName]) {
return $page->handle('display');
}
$status = 2;
if ($status == 1)
{
$nextName = 'page0';
}
elseif ($status == 2)
{
$nextName = 'page1';
}
else
{
$nextName = 'pageUnavailible';
}
$next =& $page->controller->getPage($nextName);
$next->handle('jump');
}
}
class pageUnavailibleActionBack extends HTML_QuickForm_Action_Back
{
function perform(&$page, $actionName)
{
$nextName = 'page0';
$next =& $page->controller->getPage($nextName);
$next->handle('jump');
}
}
class AdslSignup_Page extends HTML_QuickForm_Page
{
var $_serviceTypes = array(
'Home 500' =>
array(
'type' => 'home',
'desc' => '512kbps
yadda moo yadda.',
'router ' => false,
'modem' => true,
),
'Home 1000' =>
array(
'type' => 'home',
'desc' => '1024kbps
yadda moo yadda.',
'router' => true,
'modem' => true,
),
'Network 500' =>
array(
'type' => 'business',
'desc' => '512kbps
yadda moo yadda.',
'router' => true,
'modem' => true,
),
'Network 1000' =>
array(
'type' => 'business',
'desc' => '1024kbps
yadda moo yadda.',
'router' => true,
'modem' => true,
),
'Network 2000' =>
array(
'type' => 'business',
'desc' => '2048kbps
yadda moo yadda.',
'router' => true,
'modem' => true,
),
);
var $_routers = array (
'Modem' =>
array(
'type' => 'modem',
'desc' => 'This modem
is ideal of potential LART targets',
'price' => 100
),
'UberModem' =>
array(
'type' => 'modem',
'desc' => 'An
UberModem does everything, even makes coffee, and damn good coffee too',
'price' => 100
),
'Router' =>
array(
'type' => 'router',
'desc' => 'A crappy
router.',
'price' => 100
),
'UberRouter' =>
array(
'type' => 'router',
'desc' => 'A router
that not only makes coffee, but also knows how to cook the perfect fried
egg',
'price' => 100
),
);
}
class Page0 extends AdslSignup_Page
{
function buildForm()
{
$this->_formBuilt = true;
$this->addElement('header', null, 'ADSL Signup - Line
Check');
$this->addElement('text', 'bbnumber', 'Telephone
Number');
$this->addElement('checkbox', 'isdn', 'Is this line an ISDN
line?');
$this->addElement('submit', $this->getButtonName('next'),
'Next
>>');
$this->setDefaultAction('next');
}
}
class Page1 extends AdslSignup_Page
{
function buildForm()
{
$this->_formBuilt = true;
$this->addElement('header', null, 'ADSL Signup - Personal
Information');
$this->addElement('text', 'name', 'Name');
$this->addElement('text', 'company', 'Company');
$this->addElement('text', 'address1', 'Street');
$this->addElement('text', 'address2', '');
$this->addElement('text', 'address3', 'Town');
$this->addElement('text', 'address4', 'County');
$this->addElement('text', 'postcode', 'Postcode');
$this->addElement('text', 'homephone', 'Telephone');
$this->addElement('text', 'workphone', 'Work
Telephone');
$this->addElement('text', 'fax', 'Fax');
$this->addElement('text', 'email', 'EMail
Address');
$this->addElement('text', 'bbnumber', 'Number for
Broadband');
$this->addElement('checkbox', 'existing', 'Are you an existing
customer?');
$this->addElement('text', 'accountnum', 'Account
Number?');
$this->addElement('textarea', 'heard', 'Please let us know
where
you heard about us');
$radio = array();
$radio[] = $this->createElement('radio', null, null, 'Home User',
'home');
$radio[] = $this->createElement('radio', null, null, 'Business User',
'business');
$this->addGroup($radio, 'usertype', 'User Type', '<br
/>');
$this->addRule('usertype', 'Required Field', 'required');
$prevnext = array();
$prevnext[] =& $this->createElement('submit',
$this->getButtonName('back'), '<< Back');
$prevnext[] =& $this->createElement('submit',
$this->getButtonName('next'), 'Next >>');
$this->addGroup($prevnext, null, '', ' ', false);
$this->setDefaultAction('next');
}
}
class Page2 extends AdslSignup_Page
{
function buildForm()
{
$this->_formBuilt = true;
$this->addElement('header', null, 'ADSL Signup - Select a
Service');
$servicetype = $this->controller->exportValue('page1', 'usertype');
$radio = array();
foreach ($this->_serviceTypes as $key => $value)
{
if ($value['type'] == $servicetype)
{
$radio[] = $this->createElement('radio', null, null, $key, $key);
$radio[] = $this->createElement('static', null, null,
$value['desc']);
}
}
$this->addGroup($radio, 'servicetype', 'Service Type', '<br
/>');
$this->addRule('servicetype', 'Required Field', 'required');
$prevnext = array();
$prevnext[] =& $this->createElement('submit',
$this->getButtonName('back'), '<< Back');
$prevnext[] =& $this->createElement('submit',
$this->getButtonName('next'), 'Next >>');
$this->addGroup($prevnext, null, '', ' ', false);
$this->setDefaultAction('next');
}
}
class Page3 extends AdslSignup_Page
{
function buildForm()
{
$this->_formBuilt = true;
$this->addElement('header', null, 'ADSL Signup - Select your
equipment');
$servicetype = $this->controller->exportValue('page2',
'servicetype');
$radio = array();
foreach ($this->_routers as $key => $value)
{
if ($this->_serviceTypes[$servicetype][$value['type']])
{
$radio[] = $this->createElement('radio', null, null, $key, $key);
$radio[] = $this->createElement('static', null, null,
$value['desc']);
}
}
$radio[] = $this->createElement('radio', null, null, 'No Router/Modem',
'none');
$this->addGroup($radio, 'hardwaretype', 'Hardware Type', '<br
/>');
$this->addRule('hardwaretype', 'Required Field', 'required');
$prevnext = array();
$prevnext[] =& $this->createElement('submit',
$this->getButtonName('back'), '<< Back');
$prevnext[] =& $this->createElement('submit',
$this->getButtonName('next'), 'Next >>');
$this->addGroup($prevnext, null, '', ' ', false);
$this->setDefaultAction('next');
}
}
class Page4 extends AdslSignup_Page
{
function buildForm()
{
$this->_formBuilt = true;
$usertype = $this->controller->exportValue('page1', 'usertype');
$servicetype = $this->controller->exportValue('page2',
'servicetype');
$routertype = $this->controller->exportValue('page3',
'hardwaretype');
if ($this->_routers[$routertype]['type'] == 'modem')
{
$this->addElement('header', null, 'Hardware Support');
$this->addElement('checkbox', 'modemsupport', 'You\'ll
break your
modem, and we will fix it');
}
elseif ($this->_routers[$routertype]['type'] == 'router')
{
$this->addElement('header', null, 'Hardware Support');
$this->addElement('checkbox', 'routersupport', 'You\'ll
break your
router, and we will fix it');
}
else
{
// if they don't have a modem....
}
$this->addElement('header', null, 'Sexy Extras');
if ($usertype == 'business')
{
$this->addElement('checkbox', 'webdesign', 'we will make a damn
sexy
site, just for you');
$this->addElement('checkbox', 'bizemail', 'spam scanning,
we\'ll
even check in your kitchen cupboards for this foul tasting stuff');
$this->addElement('checkbox', 'commerce', 'Wanna sell stuff
online?
we\'ll make a nice pretty shop for you');
$this->addElement('checkbox', 'pro', 'Pro. £20. all that
cool
stuff. wow.');
}
else
{
// stuff specific to home users.
$this->addElement('checkbox', 'homeemail', 'spam scanning,
we\'ll
even check in your kitchen cupboards for this foul tasting stuff');
}
$prevnext = array();
$prevnext[] =& $this->createElement('submit',
$this->getButtonName('back'), '<< Back');
$prevnext[] =& $this->createElement('submit',
$this->getButtonName('next'), 'Next >>');
$this->addGroup($prevnext, null, '', ' ', false);
$this->setDefaultAction('next');
}
}
class Page5 extends AdslSignup_Page
{
function buildForm()
{
$this->_formBuilt = true;
$this->addElement('header', null, 'Direct Debit Details');
$this->addElement('text', 'dd_name', 'Name');
$this->addElement('text', 'dd_company', 'Company');
$group = array();
$group[] = $this->createElement('text', 'sort1', null,
'maxlength="2"
size="2"');
$group[] = $this->createElement('text', 'sort2', null,
'maxlength="2"
size="2"');
$group[] = $this->createElement('text', 'sort3', null,
'maxlength="2"
size="2"');
$this->addGroup($group, 'dd_sortcode','Sort Code',
'-');
$this->addElement('text', 'dd_account', 'Account
Number');
$prevnext = array();
$prevnext[] =& $this->createElement('submit',
$this->getButtonName('back'), '<< Back');
$prevnext[] =& $this->createElement('submit',
$this->getButtonName('next'), 'Next >>');
$this->addGroup($prevnext, null, '', ' ', false);
}
}
class PageUnavailible extends AdslSignup_Page
{
function buildForm()
{
$this->_formBuilt = true;
$this->addElement('header', null, 'No ADSL for you!');
$this->addElement('submit', $this->getButtonName('back'),
'<<
Back');
$this->setDefaultAction('back');
}
}
class ActionProcess extends HTML_QuickForm_Action
{
function perform(&$page, $actionName)
{
echo "Submit successful!<br>\n";
Var_Dump::display($page->controller->exportValues());
}
}
class AdslSignup_Wizard extends HTML_QuickForm_Controller
{
function AdslSignup_Wizard()
{
$this->HTML_QuickForm_Controller('AdslSignup', false);
// page one needs a custom Action handler
$page0 =& new Page0('page0');
$page0->addAction('next', new Page0ActionNext());
$this->addPage($page0);
// add remaining pages
$this->addPage(new Page1('page1'));
$this->addPage(new Page2('page2'));
$this->addPage(new Page3('page3'));
$this->addPage(new Page4('page4'));
$this->addPage(new Page5('page5'));
$pageUnavailible =& new PageUnavailible('pageUnavailible');
$pageUnavailible->addAction('back', new pageUnavailibleActionBack());
$this->addPage($pageUnavailible);
$this->addAction('display', new HTML_QuickForm_Action_Display());
$this->addAction('next', new DefaultActionNext());
$this->addAction('back', new HTML_QuickForm_Action_Back());
$this->addAction('jump', new HTML_QuickForm_Action_Jump());
$this->addAction('process', new ActionProcess());
}
}
/*********************** DISPLAY THE FORM! ****************************/
$wizard =& new AdslSignup_Wizard();
$wizard->run();
?>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
iD8DBQFAwGeGEe6ncNCcEO0RAljEAKDPZIYrkVoZUeNr4bgxdqrIkI8/PwCgl3X+
Q5ImW3F0/ksLtAcviFtRgjM=
=PHHm
-----END PGP SIGNATURE-----