diff --git a/form_example/lib/Drupal/form_example/Forms/FormExampleTutorial8.php b/form_example/lib/Drupal/form_example/Forms/FormExampleTutorial8.php index 7d878e4..740dddc 100644 --- a/form_example/lib/Drupal/form_example/Forms/FormExampleTutorial8.php +++ b/form_example/lib/Drupal/form_example/Forms/FormExampleTutorial8.php @@ -6,7 +6,6 @@ namespace Drupal\form_example\Forms; use Drupal\Core\Form\FormBase; - /** * Example 8: A simple multistep form with a Next and a Back button. * @@ -35,47 +34,16 @@ class FormExampleTutorial8 extends FormBase { * {@inheritdoc} */ public function buildForm(array $form, array &$form_state) { - // Display page 2 if $form_state['page_num'] == 2 - if (!empty($form_state['page_num']) && $form_state['page_num'] == 2) { + if (empty($form_state['page_values'])) $form_state['page_values'] = array(); + if (empty($form_state['page_num'])) $form_state['page_num'] = 1; + // Build different form fields based on the $form['page_num'] value + if($form_state['page_num'] == 2){ + $form_state['page_values'][1] = $form_state['values']; return form_example_tutorial_8_page_two($form, $form_state); } - // Otherwise we build page 1. - $form_state['page_num'] = 1; - - $form['description'] = array( - '#type' => 'item', - '#title' => t('A basic multistep form (page 1)'), - ); - - $form['first'] = array( - '#type' => 'textfield', - '#title' => t('First name'), - '#description' => "Please enter your first name.", - '#size' => 20, - '#maxlength' => 20, - '#required' => TRUE, - '#default_value' => !empty($form_state['values']['first']) ? $form_state['values']['first'] : '', - ); - $form['last'] = array( - '#type' => 'textfield', - '#title' => t('Last name'), - '#default_value' => !empty($form_state['values']['last']) ? $form_state['values']['last'] : '', - ); - $form['year_of_birth'] = array( - '#type' => 'textfield', - '#title' => "Year of birth", - '#description' => 'Format is "YYYY"', - '#default_value' => !empty($form_state['values']['year_of_birth']) ? $form_state['values']['year_of_birth'] : '', - ); - $form['next'] = array( - '#type' => 'submit', - '#value' => 'Next >>', - '#submit' => array('form_example_tutorial_8_next_submit'), - '#validate' => array('form_example_tutorial_8_next_validate'), - ); - - return $form; + // Otherwise build form page 1 + return form_example_tutorial_8_page_one($form, $form_state); } /** @@ -92,22 +60,105 @@ class FormExampleTutorial8 extends FormBase { * function. An alternate list of validation functions could have been provided * in $form['#validate']. */ - public function validateForm(array &$form, array &$form_state) {} + public function validateForm(array &$form, array &$form_state) { + if(!empty($form_state['page_num']) && $form_state['page_num'] == 1) { + $year_of_birth = $form_state['values']['year_of_birth']; + // Validate birthday year + if ($year_of_birth && ($year_of_birth < 1900 || $year_of_birth > 2000)) { + form_set_error('year_of_birth', t('Enter a year between 1900 and 2000.')); + } + } + // if (!empty($form_state['page_num']) && $form_state['page_num'] == 2) {} + } /** * {@inheritdoc} */ public function submitForm(array &$form, array &$form_state) { + // Normally, some code would go here to alter the database with the data + // collected from the form. Instead sets a message with drupal_set_message() + // to validate that the code worked. + if ($form_state['values']['op'] == t('Next >>') && !empty($form_state['values'])) { + // Values are saved for each page. + // to carry forward to subsequent pages in the form. + // and we tell FAPI to rebuild the form. + if (empty($form_state['page_values'][0])) { + $form_state['page_values'][0] = $form_state['values']; + } + // When form rebuilds, it will look at this to figure which page to build. + $form_state['page_num'] = 2; + // Rebuild the form + $form_state['rebuild'] = TRUE; + } + + if ($form_state['values']['op'] == t('<< Back')) { + $form_state['values'] = $form_state['page_values'][0]; + } + if ($form_state['values']['op'] == t('Submit')) { - drupal_set_message(t('The form has been submitted. name="@first @last", year of birth=@year_of_birth', - array('@first' => $form_state['values']['first'], '@last' => $form_state['values']['last'], '@year_of_birth' => $form_state['values']['year_of_birth']))); + + drupal_set_message( + t('The form has been submitted. name=@first @last, year of birth=@year_of_birth', + array( + '@first' => $form_state['values']['first'], + '@last' => $form_state['values']['last'], + '@year_of_birth' => $form_state['values']['year_of_birth'], + ) + ) + ); + + drupal_set_message( + t('And the favorite color is @color.'), + array( + '@color' => $form_state['values']['color'], + ) + ); + + // If we wanted to redirect on submission, set $form_state['redirect'] + // $form_state['redirect'] = 'node'; // Redirects the user to /node. } } } /** - * Returns the form for the second page of form_example_tutorial_8(). + * Returns the form for the second page of form. + */ +function form_example_tutorial_8_page_one($form, &$form_state) { + $form['description'] = array( + '#type' => 'item', + '#title' => t('A basic multistep form (page 1)'), + ); + + $form['first'] = array( + '#type' => 'textfield', + '#title' => t('First name'), + '#description' => "Please enter your first name.", + '#size' => 20, + '#maxlength' => 20, + '#required' => TRUE, + '#default_value' => !empty($form_state['values']['first']) ? $form_state['values']['first'] : '', + ); + $form['last'] = array( + '#type' => 'textfield', + '#title' => t('Last name'), + '#default_value' => !empty($form_state['values']['last']) ? $form_state['values']['last'] : '', + ); + $form['year_of_birth'] = array( + '#type' => 'textfield', + '#title' => "Year of birth", + '#description' => 'Format is "YYYY"', + '#default_value' => !empty($form_state['values']['year_of_birth']) ? $form_state['values']['year_of_birth'] : '', + ); + $form['next'] = array( + '#type' => 'submit', + '#value' => 'Next >>', + ); + return $form; +} + +/** + * Returns the form for the second page of form */ function form_example_tutorial_8_page_two($form, &$form_state) { $form['description'] = array( @@ -124,48 +175,15 @@ function form_example_tutorial_8_page_two($form, &$form_state) { $form['submit'] = array( '#type' => 'submit', '#value' => t('Submit'), - '#submit' => array('form_example_tutorial_8_page_two_submit'), + // '#submit' => array('form_example_tutorial_8_page_two_submit'), ); $form['back'] = array( '#type' => 'submit', '#value' => t('<< Back'), - '#submit' => array('form_example_tutorial_8_page_two_back'), + // '#submit' => array('form_example_tutorial_8_page_two_back'), // We won't bother validating the required 'color' field, since they // have to come back to this page to submit anyway. '#limit_validation_errors' => array(), ); return $form; } - - -/** - * Validate handler for the next button on first page. - */ -function form_example_tutorial_8_next_validate($form, &$form_state) { - $year_of_birth = $form_state['values']['year_of_birth']; - if ($year_of_birth && ($year_of_birth < 1900 || $year_of_birth > 2000)) { - form_set_error('year_of_birth', t('Enter a year between 1900 and 2000.')); - } -} - -/** - * Submit handler for form_example_tutorial_8() next button. - * - * Capture the values from page one and store them away so they can be used - * at final submit time. - */ -function form_example_tutorial_8_next_submit($form, &$form_state) { - - // Values are saved for each page. - // to carry forward to subsequent pages in the form. - // and we tell FAPI to rebuild the form. - $form_state['page_values'][1] = $form_state['values']; - - if (!empty($form_state['page_values'][2])) { - $form_state['values'] = $form_state['page_values'][2]; - } - - // When form rebuilds, it will look at this to figure which page to build. - $form_state['page_num'] = 2; - $form_state['rebuild'] = TRUE; -}