/[drupal]/contributions/docs/developer/examples/multipage_form_example.module
ViewVC logotype

Diff of /contributions/docs/developer/examples/multipage_form_example.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.7, Sun Apr 2 19:23:47 2006 UTC revision 1.8, Sat Apr 8 03:35:39 2006 UTC
# Line 1  Line 1 
1  <?php  <?php
2    
3  // $Id: multipage_form_example.module,v 1.6 2006/04/02 19:02:55 thehunmonkgroup Exp $  // $Id: multipage_form_example.module,v 1.7 2006/04/02 19:23:47 thehunmonkgroup Exp $
4    
5  /**  /**
6   * Implementation of hook_help().   * Implementation of hook_help().
# Line 147  function multipage_form_example_form(&$n Line 147  function multipage_form_example_form(&$n
147      '#required'      => TRUE,      '#required'      => TRUE,
148    );    );
149    
   // Some markup elements to help the user see what they've already  
   // entered.  Since the data won't be available until after the form  
   // is submitted, we'll only build the basic element here, and then  
   // we'll populate it in #pre_render  
   $form['person_display'] = array(  
     '#type'          => 'markup',  
   );  
   $form['color_number_display'] = array(  
     '#type'          => 'markup',  
   );  
   
150    // Add a back button    // Add a back button
151    $form['back'] = array(    $form['back'] = array(
152      '#type'          => 'button',      '#type'          => 'button',
# Line 274  function multipage_form_example_pre_rend Line 263  function multipage_form_example_pre_rend
263    
264      // Movie and tv show, page 4      // Movie and tv show, page 4
265      multipage_form_set_element_visibility($form['fav_movie'],                 in_array($form['page']['#value'], array(4)));      multipage_form_set_element_visibility($form['fav_movie'],                 in_array($form['page']['#value'], array(4)));
266      // This bit of trickery is because radios is a bit of a special case --      multipage_form_set_element_visibility($form['fav_tv'],                    in_array($form['page']['#value'], array(4)), $next_page);
     // it's an 'expanding' form element, which means that the multiple buttons  
     // get built by form_builder. Because the building for display actually  
     // happens when the form is still set to stage 3, we have to build it as a  
     // radios type in stage 3, or nothing will display on stage 4!  
     // form_hide_elements is a little helper function that recurses through and  
     // hides all of the buttons we built if we don't actually happen to be  
     // displaying stage 4.  whew!  
     if ($next_page) {  
       multipage_form_set_element_visibility($form['fav_tv'],                  in_array($form['page']['#value'], array(4)));  
     }  
267    
268      // Buttons -- like radios, they need to be properly built in the previous stage, so here we also do the switches at      // Buttons
269      // the last chance we have.  Notice that the type gets set to 'value' and not 'hidden', b/c hidden will screw w/      multipage_form_set_element_visibility($form['back'],                      in_array($form['page']['#value'], array(2, 3, 4)), $next_page);
270      // $_POST['op']      multipage_form_set_element_visibility($form['preview'],                   in_array($form['page']['#value'], array(4)), $next_page);
271        multipage_form_set_element_visibility($form['submit'],                    in_array($form['page']['#value'], array(4)), $next_page);
272    
273        // The button text actually helps determine if a form has actually been
274        // submitted because the name is also the value of a clicked button. By
275        // changing it for building, but not rendering, the form is not fully
276        // sumbitted until we name it 'Submit', the usual value.
277      if ($next_page) {      if ($next_page) {
       multipage_form_set_element_visibility($form['back'],                  in_array($form['page']['#value'], array(2, 3, 4)));  
       multipage_form_set_element_visibility($form['preview'],               in_array($form['page']['#value'], array(4)));  
       multipage_form_set_element_visibility($form['submit'],                in_array($form['page']['#value'], array(4)));  
278        $submit_text = array(NULL, t('Next (person)'), t('Next (color/number)'), t('Next (tv/movie)'), t('Submit'));        $submit_text = array(NULL, t('Next (person)'), t('Next (color/number)'), t('Next (tv/movie)'), t('Submit'));
279        $form['submit']['#value'] = $submit_text[$form['page']['#value']];        $form['submit']['#value'] = $submit_text[$form['page']['#value']];
280      }      }
# Line 393  function multipage_form_set_element_visi Line 375  function multipage_form_set_element_visi
375      switch ($element['#type']) {      switch ($element['#type']) {
376        case 'textfield':        case 'textfield':
377        case 'textarea':        case 'textarea':
       case 'radios':  
378        case 'select':        case 'select':
379          multipage_form_set_attribute($element, '#type', 'hidden');          multipage_form_set_attribute($element, '#type', 'hidden');
380          multipage_form_set_attribute($element, '#required', FALSE);          multipage_form_set_attribute($element, '#required', FALSE);
381          break;          break;
382    
383          case 'radios':
384            // Radios elements cannot be hidden unless they have been processed.
385            if ($next_page) {
386              multipage_form_set_attribute($element, '#type', 'hidden');
387              multipage_form_set_attribute($element, '#required', FALSE);
388            }
389            break;
390    
391        case 'radio':        case 'radio':
392        case 'checkbox':        case 'checkbox':
   
393          // We can't change these to hidden until right before the next page is rendered, otherwise          // We can't change these to hidden until right before the next page is rendered, otherwise
394          // the value is lost sometimes.          // the value is lost sometimes.
395          if ($next_page) {          if ($next_page) {
# Line 414  function multipage_form_set_element_visi Line 402  function multipage_form_set_element_visi
402          break;          break;
403    
404        case 'button':        case 'button':
405          multipage_form_set_attribute($element, '#type', 'value');          if ($next_page) {
406              multipage_form_set_attribute($element, '#type', 'value');
407            }
408          break;          break;
409    
410        case 'submit':        case 'submit':
411          multipage_form_set_attribute($element, '#type', 'button');          if ($next_page) {
412              multipage_form_set_attribute($element, '#type', 'button');
413            }
414          break;          break;
415      }      }
416    }    }
417    
418    foreach (element_children($element) as $key) {    foreach (element_children($element) as $key) {
419      multipage_form_set_element_visibility($element[$key], $visible);      multipage_form_set_element_visibility($element[$key], $visible, $next_page);
420    }    }
421  }  }
422    

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.8

  ViewVC Help
Powered by ViewVC 1.1.2