Same filename and directory in other branches

This is the Form API Tutorial from the handbook.

It goes through several form examples of increasing complexity to demonstrate Drupal 7 Form API.

Links are provided inline for the related handbook pages.

See also

http://drupal.org/node/262422

File

form_example/form_example_tutorial.inc
View source

Functions

Namesort descendingDescription
form_example_tutorialMain Form tutorial page.
form_example_tutorial_1Tutorial Example 1.
form_example_tutorial_10Example 10: A form with a file upload field.
form_example_tutorial_10_submitSubmit handler for form_example_tutorial_10().
form_example_tutorial_10_validateValidate handler for form_example_tutorial_10().
form_example_tutorial_11Example 11: adding a confirmation form.
form_example_tutorial_11_confirm_nameExample 11: A form generated with confirm_form().
form_example_tutorial_11_confirm_name_submitSubmit function for form_example_tutorial_11_confirm_form().
form_example_tutorial_11_submitSubmit function for form_example_tutorial_11().
form_example_tutorial_2This is Example 2, a basic form with a submit button.
form_example_tutorial_3Example 3: A basic form with fieldsets.
form_example_tutorial_4Example 4: Basic form with required fields.
form_example_tutorial_5Example 5: Basic form with additional element attributes.
form_example_tutorial_6Example 6: A basic form with a validate handler.
form_example_tutorial_6_validateValidation handler for Tutorial 6.
form_example_tutorial_7Example 7: With a submit handler.
form_example_tutorial_7_submitSubmit function for form_example_tutorial_7().
form_example_tutorial_7_validateValidation function for form_example_tutorial_7().
form_example_tutorial_8Example 8: A simple multistep form with a Next and a Back button.
form_example_tutorial_8_next_submitSubmit handler for form_example_tutorial_8() next button.
form_example_tutorial_8_next_validateValidate handler for the next button on first page.
form_example_tutorial_8_page_twoReturns the form for the second page of form_example_tutorial_8().
form_example_tutorial_8_page_two_backBack button handler submit handler.
form_example_tutorial_8_page_two_submitThe page 2 submit handler.
form_example_tutorial_9Example 9: A form with a dynamically added new fields.
form_example_tutorial_9_add_nameSubmit handler for "Add another name" button on form_example_tutorial_9().
form_example_tutorial_9_remove_nameSubmit handler for "Remove name" button on form_example_tutorial_9().
form_example_tutorial_9_submitSubmit function for form_example_tutorial_9().
form_example_tutorial_9_validateValidate function for form_example_tutorial_9().

Comments

hmdnawaz’s picture

I have a module installed. I want to add an additional submit handler in to submit function of the module. for example I can add additional submit handler to a function with the following code in drupal 6.

function test_form_alter(&$form, &$form_state, $form_id) {

  switch ($form_id) {
    case "form id":
      $form['#submit'][] = 'test_community_submit';
      break;
  }
}

function test_community_submit($form, &$form_state) {
  // Do some stuff here.
}

How can achieve the above functionality in drupal 7?

Atomox’s picture

The difference between form_state['values'] VS form_state['input'] should really be included in at least one of these examples, as this functionality, as written, is misleading.

You can reference your data from the $form_state object, as displayed in these examples. Consider:

$form['title_field'] = array(
    '#type' => 'textfield',
    '#title' => t('My Title'),
  );

You can reference this value in the validation or other functions by accessing the $form_state array, as such:

$my_title_value = $form_state['values']['title_field'];

This is shown in several examples. *However,* if you pre-populate a form with a value, like so:

$form['title_field'] = array(
    '#type' => 'textfield',
    '#title' => t('My Title'),
    '#value' => 'My Title',
  );

then trying to get the value with the method above will return 'My Title' *instead* of the user-modified value. To get the modified value, in my experience, you must do it like so:

$my_title_value = $form_state['input']['title_field'];

This should return the user-supplied value you're more likely looking for. Note that using $form['title_field']['#value'] *also* returns the value *before* the user modified it. This is not obvious, and it would make sense to clarify this in the example module.

aBrookland’s picture

I know this is old but you shouldn't be setting #value here which is why you're getting the wrong result in $form_state['values'].

If you want to provide an initial value that the user can then modify you should set #default_value, then the correct user entered value will be in $form_state['values'].

sanguis’s picture

They are all out of order,
1 should become 01 2 should become 02 ect...

jlscott’s picture

My experience has shown that spaces in the form element names will cause the entered values to be missing from the $form_state['values'] array. Refer to my comment at https://drupal.org/comment/8285811#comment-8285811.