View Single Post
  #2 (permalink)  
Old 04-09-2008, 09:43 AM
MiK MiK is offline
Junior Member
 
Join Date: Mar 2008
Location: Ancona, Italy
Posts: 21
Send a message via MSN to MiK
Default Solution found (almost working)

Hello,
Since i haven't found any solution to my issue, i managed to write something by myself.

I wrote an external extensible class, which provides (or tries to do so) the following features:

- Simple multi page form handling (all forms are extensions of my class)
- Data persistence using sessions
- Arbitrary step retrieving (in case you would edit some previous submitted step)
- Data validation/filtering for each step
- Stored data retrieving/deleting for each step

But does NOT (yet?) provide the following:

- Multi instance form handling (when the user opens multiple instances of the same form, using tabs for example, this uses the same session.. can't find a workaround for this)
- Conditional step retrieving (i don't need this, at the moment, neither i've got any idea on how implement this)
- Something else

Dependencies:
- Zend Framework version 1.5.1
- Zend_Form_*
- Zend_Session_*
- Zend_Controller_Request_Http

Here's a code snippet from my controller, just to let you see how it works:
PHP Code:
class TestController extends Controller
{
    function 
testAction()
    {
        require_once(
'Classes/Forms/MyForm.php');
        
$form = new Form_MyForm();

        
$form->setStepAction('/test/test')
             ->
setStepMethod('post')
             ->
setStepAttrib('enctype''multipart/form-data')
             ->
setStepAttrib('id''myform')
             ->
setStepPrefix('step_')
             ->
setSessionNamespace('mysession')
             ->
build();

        
$request $this->getRequest();

        
// If this is a POST request ...
        
if ($request->isPost()) {
            
// i get the submitted form id
            
$step $form->getCurrentStepId($request);

            
$data $request->getPost();

            
// if submitted step data is valid ..
            
if ($form->isValidStep($step$data)) {
                
// store data into the session
                
$form->saveStep($step);
                
// get the next step available
                
$currentStep $form->getNextStep($step);
                
// if there's no next step ...
                
if ($currentStep === false) {
                    
// ... we reached the end and we should handle form data
                    
Zend_Debug::dump$form->getValues(), "FORM COMPLETED");
                    exit;
                }
            
// if data is not valid, we get the same form again
            
} else {
                
$currentStep $form->getStep($step);

            }
        
// If this is a normal GET request (/test/test/step/:id)
        
} else {
            
// We get the step id requested by the user
            
$step = (int) $request->getParam('step');
            
            
// If no step is requested, we assume the first step
            // and reset session data. 
            
if ($step == 0) {
                
$step 1;
                
$form->clearSession();
            }
            
            
// Gets the requested step (if exists)
            
if ($currentStep $form->getStep($step)) {
                
// If there's pre-submitted data in session, we use id
                // to repopulate form
                
$form->populateStep($step);
                
// Deletes data from session
                // (user should re-submit form again)
                
$form->deleteStep($step);
            
// This is just an error sample, we may throw an exception...
            
} else {
                
$currentStep "STEP REQUESTED IS NOT VALID";
            }
        }
       
        
// I use Smarty as View Renderer
        
$this->view->form $currentStep;
        echo 
$this->view->render('testform.tpl');
    }

If someone is interested, i will post the rest of the code (class, and form example).

Just a little disclaimer:
Quote:
I am not a zend framework expert, nor a professional php developer.
This is just an attempt to make the process of creating multi page forms, a bit less
tedious and a bit more maintenable. The code may be not written at best,
and requires improvements for sure.

Last edited by MiK : 04-10-2008 at 01:37 PM.
Reply With Quote