Functions to enable the processing and display of HTML forms.
Drupal uses these functions to achieve consistency in its form processing and presentation, while simplifying code and reducing the amount of HTML that must be explicitly generated by modules.
The primary function used with forms is drupal_get_form(), which is used for forms presented interactively to a user. Forms can also be built and submitted programmatically without any user input using the drupal_form_submit() function.
drupal_get_form() handles retrieving, processing, and displaying a rendered HTML form for modules automatically.
Here is an example of how to use drupal_get_form() and a form builder function:
<?php
$form = drupal_get_form('my_module_example_form');
...
function my_module_example_form($form, &$form_state) {
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
function my_module_example_form_validate($form, &$form_state) {
// Validation logic.
}
function my_module_example_form_submit($form, &$form_state) {
// Submission logic.
}
?>Or with any number of additional arguments:
<?php
$extra = "extra";
$form = drupal_get_form('my_module_example_form', $extra);
...
function my_module_example_form($form, &$form_state, $extra) {
$form['submit'] = array(
'#type' => 'submit',
'#value' => $extra,
);
return $form;
}
?>The $form argument to form-related functions is a structured array containing the elements and properties of the form. For information on the array components and format, and more detailed explanations of the Form API workflow, see the Form API reference and the Form API section of the handbook. In addition, there is a set of Form API tutorials in the Form Example Tutorial which provide basics all the way up through multistep forms.
In the form builder, validation, submission, and other form functions, $form_state is the primary influence on the processing of the form and is passed by reference to most functions, so they use it to communicate with the form system and each other.
The $form_state keys are:
| Name | Location | Description |
|---|---|---|
| date_validate | drupal-7/includes/form.inc | Validates the date type to stop dates like February 30, 2006. |
| drupal_build_form | drupal-7/includes/form.inc | Build and process a form based on a form id. |
| drupal_form_submit | drupal-7/includes/form.inc | Retrieves, populates, and processes a form. |
| drupal_get_form | drupal-7/includes/form.inc | Wrapper for drupal_build_form() for use when $form_state is not needed. |
| drupal_prepare_form | drupal-7/includes/form.inc | Prepares a structured form array by adding required elements, executing any hook_form_alter functions, and optionally inserting a validation token to prevent tampering. |
| drupal_process_form | drupal-7/includes/form.inc | Processes a form submission. |
| drupal_rebuild_form | drupal-7/includes/form.inc | Retrieves a form, caches it and processes it again. |
| drupal_redirect_form | drupal-7/includes/form.inc | Redirects the user to a URL after a form has been processed. |
| drupal_retrieve_form | drupal-7/includes/form.inc | Retrieves the structured array that defines a given form. |
| drupal_validate_form | drupal-7/includes/form.inc | Validates user-submitted form data from the $form_state using the validate functions defined in a structured form array. |
| form_builder | drupal-7/includes/form.inc | Walk through the structured form array, adding any required properties to each element and mapping the incoming input data to the proper elements. Also, execute any #process handlers attached to a specific element. |
| form_clear_error | drupal-7/includes/form.inc | Clear all errors against all form elements made by form_set_error(). |
| form_error | drupal-7/includes/form.inc | Flag an element as having an error. |
| form_execute_handlers | drupal-7/includes/form.inc | A helper function used to execute custom validation and submission handlers for a given form. Button-specific handlers are checked first. If none exist, the function falls back to form-level handlers. |
| form_get_cache | drupal-7/includes/form.inc | Fetch a form from cache. |
| form_get_error | drupal-7/includes/form.inc | Return the error message filed against the form with the specified name. |
| form_get_errors | drupal-7/includes/form.inc | Return an associative array of all errors. |
| form_get_options | drupal-7/includes/form.inc | Traverses a select element's #option array looking for any values that hold the given key. Returns an array of indexes that match. |
| form_options_flatten | drupal-7/includes/form.inc | Allows PHP array processing of multiple select options with the same value. |
| form_pre_render_conditional_form_element | drupal-7/includes/form.inc | Add form_element theming to an element if title or description is set. |
| form_pre_render_fieldset | drupal-7/includes/form.inc | Adds members of this group as actual elements for rendering. |
| form_process_actions | drupal-7/includes/form.inc | Processes a form actions container element. |
| form_process_container | drupal-7/includes/form.inc | Processes a container element. |
| form_process_date | drupal-7/includes/form.inc | Roll out a single date element. |
| form_process_fieldset | drupal-7/includes/form.inc | Adds fieldsets to the specified group or adds group members to this fieldset. |
| form_process_password_confirm | drupal-7/includes/form.inc | Expand a password_confirm field into two text boxes. |
| form_process_radios | drupal-7/includes/form.inc | Roll out a single radios element to a list of radios, using the options array as index. |
| form_process_tableselect | drupal-7/includes/form.inc | Create the correct amount of checkbox or radio elements to populate the table. |
| form_process_vertical_tabs | drupal-7/includes/form.inc | Creates a group formatted as vertical tabs. |
| form_process_weight | drupal-7/includes/form.inc | Expand weight elements into selects. |
| form_select_options | drupal-7/includes/form.inc | Converts a select form element's options array into an HTML. |
| form_set_cache | drupal-7/includes/form.inc | Store a form in the cache. |
| form_set_error | drupal-7/includes/form.inc | Files an error against a form element. |
| form_set_value | drupal-7/includes/form.inc | Change submitted form values during form validation. |
| form_state_defaults | drupal-7/includes/form.inc | Retrieve default values for the $form_state array. |
| form_state_keys_no_cache | drupal-7/includes/form.inc | Returns an array of $form_state keys that shouldn't be cached. |
| form_state_values_clean | drupal-7/includes/form.inc | Removes internal Form API elements and buttons from submitted form values. |
| form_type_checkboxes_value | drupal-7/includes/form.inc | Helper function to determine the value for a checkboxes form element. |
| form_type_checkbox_value | drupal-7/includes/form.inc | Helper function to determine the value for a checkbox form element. |
| form_type_image_button_value | drupal-7/includes/form.inc | Helper function to determine the value for an image button form element. |
| form_type_password_confirm_value | drupal-7/includes/form.inc | Helper function to determine the value for a password_confirm form element. |
| form_type_select_value | drupal-7/includes/form.inc | Helper function to determine the value for a select form element. |
| form_type_textfield_value | drupal-7/includes/form.inc | Helper function to determine the value for a textfield form element. |
| form_type_token_value | drupal-7/includes/form.inc | Helper function to determine the value for form's token value. |
| map_month | drupal-7/includes/form.inc | Helper function for usage with drupal_map_assoc to display month names. |
| password_confirm_validate | drupal-7/includes/form.inc | Validate password_confirm element. |
| theme_button | drupal-7/includes/form.inc | Returns HTML for a button form element. |
| theme_checkbox | drupal-7/includes/form.inc | Returns HTML for a checkbox form element. |
| theme_checkboxes | drupal-7/includes/form.inc | Returns HTML for a set of checkbox form elements. |
| theme_container | drupal-7/includes/form.inc | Returns HTML for a container for grouped form items. |
| theme_date | drupal-7/includes/form.inc | Returns HTML for a date selection form element. |
| theme_fieldset | drupal-7/includes/form.inc | Returns HTML for a fieldset form element and its children. |
| theme_file | drupal-7/includes/form.inc | Returns HTML for a file upload form element. |
| theme_form | drupal-7/includes/form.inc | Returns HTML for a form. |
| theme_form_element | drupal-7/includes/form.inc | Returns HTML for a form element. |
| theme_form_element_label | drupal-7/includes/form.inc | Returns HTML for a form element label and required marker. |
| theme_form_required_marker | drupal-7/includes/form.inc | Returns HTML for a marker for required form elements. |
| theme_hidden | drupal-7/includes/form.inc | Returns HTML for a hidden form element. |
| theme_image_button | drupal-7/includes/form.inc | Returns HTML for an image button form element. |
| theme_password | drupal-7/includes/form.inc | Returns HTML for a password form element. |
| theme_radio | drupal-7/includes/form.inc | Returns HTML for a radio button form element. |
| theme_radios | drupal-7/includes/form.inc | Returns HTML for a set of radio button form elements. |
| theme_select | drupal-7/includes/form.inc | Returns HTML for a select form element. |
| theme_submit | drupal-7/includes/form.inc | Returns HTML for a submit button form element. |
| theme_tableselect | drupal-7/includes/form.inc | Returns HTML for a table with radio buttons or checkboxes. |
| theme_textarea | drupal-7/includes/form.inc | Returns HTML for a textarea form element. |
| theme_textfield | drupal-7/includes/form.inc | Returns HTML for a textfield form element. |
| theme_vertical_tabs | drupal-7/includes/form.inc | Returns HTML for an element's children fieldsets as vertical tabs. |
| weight_value | drupal-7/includes/form.inc | If no default value is set for weight select boxes, use 0. |
| _form_builder_handle_input_element | drupal-7/includes/form.inc | Populate the #value and #name properties of input elements so they can be processed and rendered. |
| _form_button_was_clicked | drupal-7/includes/form.inc | Helper function to handle the convoluted logic of button click detection. |
| _form_element_triggered_scripted_submission | drupal-7/includes/form.inc | Helper function to handle the convoluted logic of button click detection. |
| _form_options_flatten | drupal-7/includes/form.inc | Helper function for form_options_flatten(). |
| _form_set_class | drupal-7/includes/form.inc | Sets a form element's class attribute. |
| _form_set_value | drupal-7/includes/form.inc | Helper function for form_set_value() and _form_builder_handle_input_element(). |
| _form_validate | drupal-7/includes/form.inc | Performs validation on form elements. First ensures required fields are completed, #maxlength is not exceeded, and selected options were in the list of options given to the user. Then calls user-defined validators. |