Form generation

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 drupal_get_form() function handles retrieving, processing, and displaying a rendered HTML form for modules automatically. For example:

<?php

// Display the user registration form.
$output = drupal_get_form('user_register');

?>

Forms can also be built and submitted programmatically without any user input using the drupal_execute() function.

For information on the format of the structured arrays used to define forms, and more detailed explanations of the Form API workflow, see the reference and the quickstart guide.

Functions

NameLocationDescription
checkboxes_valueincludes/form.incHelper function to load value from default value for checkboxes.
date_validateincludes/form.incValidates the date type to stop dates like February 30, 2006.
drupal_executeincludes/form.incRetrieves a form using a form_id, populates it with $form_values, processes it, and returns any validation errors encountered. This function is the programmatic counterpart to drupal_get_form().
drupal_get_formincludes/form.incRetrieves a form from a builder function, passes it on for processing, and renders the form or redirects to its destination as appropriate. In multi-step form scenarios, it handles properly processing the values using the previous step's form...
drupal_prepare_formincludes/form.incPrepares 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_formincludes/form.incThis function is the heart of form API. The form gets built, validated and in appropriate cases, submitted.
drupal_redirect_formincludes/form.incRedirect the user to a URL after a form has been processed.
drupal_render_formincludes/form.incRenders a structured form array into themed HTML.
drupal_retrieve_formincludes/form.incRetrieves the structured array that defines a given form.
drupal_submit_formincludes/form.incProcesses user-submitted form data from a global variable using the submit functions defined in a structured form array.
drupal_validate_formincludes/form.incValidates user-submitted form data from a global variable using the validate functions defined in a structured form array.
expand_dateincludes/form.incRoll out a single date element.
expand_password_confirmincludes/form.incExpand a password_confirm field into two text boxes.
expand_radiosincludes/form.incRoll out a single radios element to a list of radios, using the options array as index.
form_builderincludes/form.incAdds some required properties to each form element, which are used internally in the form API. This function also automatically assigns the value property from the $edit array, provided the element doesn't already have an assigned value.
form_clean_idincludes/form.incRemove invalid characters from an HTML ID attribute string.
form_errorincludes/form.incFlag an element as having an error.
form_get_errorincludes/form.incReturn the error message filed against the form with the specified name.
form_get_errorsincludes/form.incReturn an associative array of all errors.
form_get_optionsincludes/form.incTraverses a select element's #option array looking for any values that hold the given key. Returns an array of indexes that match.
form_set_errorincludes/form.incFile an error against a form element. If the name of the element is edit[foo][bar] then you may pass either foo or foo][bar as $name foo will set an error for all its children.
form_set_valueincludes/form.incUse this function to make changes to form values in the form validate phase, so they will be available in the submit phase in $form_values.
map_monthincludes/form.incHelper function for usage with drupal_map_assoc to display month names.
password_confirm_validateincludes/form.incValidate password_confirm element.
process_weightincludes/form.incExpand weight elements into selects.
theme_checkboxincludes/form.incFormat a checkbox.
theme_checkboxesincludes/form.incFormat a set of checkboxes.
theme_dateincludes/form.incFormat a date selection element.
theme_fieldsetincludes/form.incFormat a group of form items.
theme_fileincludes/form.incFormat a file upload field.
theme_formincludes/form.incFormat a form.
theme_form_elementincludes/form.incReturn a themed form element.
theme_hiddenincludes/form.incFormat a hidden form field.
theme_itemincludes/form.incFormat a form item.
theme_passwordincludes/form.incFormat a password field. * *
theme_password_confirmincludes/form.incFormat a password_confirm item.
theme_radioincludes/form.incFormat a radio button.
theme_radiosincludes/form.incFormat a set of radio buttons.
theme_selectincludes/form.incFormat a dropdown menu or scrolling selection box.
theme_textareaincludes/form.incFormat a textarea.
theme_textfieldincludes/form.incFormat a textfield.
weight_valueincludes/form.incIf no default value is set for weight select boxes, use 0.
_drupal_clean_form_sessionsincludes/form.incRemove form information that's at least a day old from the $_SESSION['form'] array.
_element_infoincludes/form.incRetrieve the default properties for the defined element type.
_form_set_classincludes/form.incSets a form element's class attribute.
_form_set_valueincludes/form.incHelper function for form_set_value().
_form_validateincludes/form.incPerforms 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.
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.