Re: [fw-general] Zend_Form multipage examples?
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: _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' ) ),
Re: [fw-general] Zend_Form multipage examples?
-- Justin Hendrickson <[EMAIL PROTECTED]> wrote (on Tuesday, 12 February 2008, 04:09 PM -0600): > 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. It's actually not even implemented. :-) (note to self: either remove, or raise a warning in that method...) -- Matthew Weier O'Phinney PHP Developer| [EMAIL PROTECTED] Zend - The PHP Company | http://www.zend.com/
Re: [fw-general] Zend_Form multipage examples?
> > 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: _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 (
Re: [fw-general] Zend_Form multipage examples?
Or you can use Jurrien's proposal code as a starting point :) See http://framework.zend.com/wiki/pages/viewpage.action?pageId=42130 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. -- 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
Re: [fw-general] Zend_Form multipage examples?
-- Justin Hendrickson <[EMAIL PROTECTED]> wrote (on Tuesday, 12 February 2008, 01:56 PM -0600): > 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. I'm aware of that; I haven't had a chance to work up more advanced use cases and examples for this bit of functionality yet. A note or two below: > 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: > > 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( 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. > 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' =