I'm trying to put together a simple Zend_Form setup for a multipaged form,
but the documentation on implementing the multipage part is a bit sparse.
Once the form object is setup with the subforms, what's the "right way" to
render and validate the subforms and process the results?
This is what I have right now:
<?php
require_once 'ApplicationController.php';
require_once 'Zend/Form.php';
class IndexController extends ApplicationController
{
public function siteInformationAction()
{
$subform = $this->_getForm()->getSubForm('site_information');
if ($this->getRequest()->isPost() &&
$subform->isValid($this->getRequest()->getPost())) {
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())) {
var_dump($this->_getForm()->getValues());
exit;
}
$this->view->form = $subform;
$this->render('form');
}
private function _getForm()
{
return new Zend_Form(array(
'method' => 'POST',
'subforms' => array(
'site_information' => new Zend_Form(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'
)
)
)
)),
'personal_information' => new Zend_Form(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'
)
)
)
))
)
));
}
}
Unfortunately, I'm obviously missing something because
var_dump($this->_getForm()->getValues());
is giving me array()
. Is my approach completely off? Also, how do you go about preventing
someone from manually going to the personalInformationAction()?