Hi Justin
On top of what you have here for persisting data from form submission,
the helper proposal allows you to specify the order in which forms are
processed. So you can prevent someone from jumping straight to the
'personal' action if the 'siteinformation' form has not yet been
validated.
That, and it also interacts with the redirector to allow you to
specify a destination action using the key names of form submit
elements (_next, _back, _submit, etc..) if you're operating a 'wizard'-
style or even tabbed-style interface.
Cheers
You'll probably want to store the data from validating the subform in
your session so you can retrieve it later; otherwise, your data will
be
lost. There's a variety of ways to do that -- using $_SESSION
directly,
or using Zend_Session -- so I won't go into details.
I just found the persistData(), thought it doesn't appear to be used
anywhere. I did some more hacking and added Zend_Session_Namespace
into the mix. I'll check out the Helper proposal to see if I'm
missing anything.
In case anyone wanted to see my current, semi-working example, here
ya go:
<?php
require_once 'ApplicationController.php';
require_once 'Zend/Form.php';
require_once 'Zend/Session/Namespace.php';
class IndexController extends ApplicationController
{
public function siteInformationAction()
{
$subform = $this->_getForm()->getSubForm('site_information');
if ($this->getRequest()->isPost() && $subform->isValid($this-
>getRequest()->getPost())) {
$subform->persistData();
return $this->getHelper('redirector')->gotoRoute(
array(
'action' => 'personalInformation'
)
);
}
$this->view->form = $subform;
$this->render('form');
}
public function personalInformationAction()
{
$subform = $this->_getForm()-
>getSubForm('personal_information');
if ($this->getRequest()->isPost() && $subform->isValid($this-
>getRequest()->getPost())) {
$subform->persistData();
$this->_getForm()->process();
exit;
}
$this->view->form = $subform;
$this->render('form');
}
private function _getForm()
{
return new Site_Form_Create();
}
}
class Site_Form_Create extends Zend_Form
{
public function __construct()
{
parent::__construct(array(
'method' => 'POST',
'subforms' => array(
'site_information' => new
Site_Form_Create_SiteInformation(),
'personal_information' => new
Site_Form_Create_PersonalInformation()
)
));
}
public function process()
{
foreach ($this->getSubForms() as $subform) {
var_dump($subform->getValues());
}
}
}
class Site_Form_Create_SiteInformation extends Zend_Form
{
public function __construct()
{
parent::__construct(array(
'elements' => array(
'username' => array(
'text',
array(
'label' => 'Username',
'required' => true,
'validators' => array(
'NotEmpty'
)
)
),
'password' => array(
'password',
array(
'label' => 'Password',
'required' => true,
'validators' => array(
'NotEmpty'
)
)
),
'submit_site_information' => array(
'submit',
array(
'value' => 'Next Page'
)
)
)
));
foreach ($this->_getSession() as $key => $value) {
$this->getElement($key)->setValue($value);
}
}
public function persistData()
{
$session = $this->_getSession();
foreach ($this->getValues() as $key => $value) {
$session->$key = $value;
}
}
private function _getSession()
{
return new
Zend_Session_Namespace('Site_Form_Create_SiteInformation');
}
}
class Site_Form_Create_PersonalInformation extends Zend_Form
{
public function __construct()
{
parent::__construct(array(
'elements' => array(
'name' => array(
'text',
array(
'label' => 'Name',
'required' => true,
'validators' => array(
'NotEmpty'
)
)
),
'street' => array(
'text',
array(
'label' => 'Street',
)
),
'city' => array(
'text',
array(
'label' => 'City'
)
),
'state' => array(
'text',
array(
'label' => 'State'
)
),
'zip' => array(
'text',
array(
'label' => 'Zip'
)
),
'submit_personal_information' => array(
'submit',
array(
'value' => 'Submit'
)
)
)
));
foreach ($this->_getSession() as $key => $value) {
$this->getElement($key)->setValue($value);
}
}
public function persistData()
{
$session = $this->_getSession();
foreach ($this->getValues() as $key => $value) {
$session->$key = $value;
}
}
private function _getSession()
{
return new
Zend_Session_Namespace('Site_Form_Create_PersonalInformation');
}
}
--
Simon Mundy | Director | PEPTOLAB
""" " "" """""" "" "" """"""" " "" """"" " """"" " """""" "" "
202/258 Flinders Lane | Melbourne | Victoria | Australia | 3000
Voice +61 (0) 3 9654 4324 | Mobile 0438 046 061 | Fax +61 (0) 3 9654
4124
http://www.peptolab.com