Drupal7 API Form generation

来源:互联网 发布:淘宝什么皮肤是真的 编辑:程序博客网 时间:2024/06/04 08:50

Form generation

  1. 8.x drupal/core/core.api.php form_api
  2. 6.x drupal/includes/form.inc form_api
  3. 7.x drupal/includes/form.inc form_api

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:

$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:

$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 documentation section. 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.

See drupal_build_form() for documentation of $form_state keys.

文件

drupal/includes/form.inc, line 25
Functions for form and batch generation and processing.

函数

名字降序排列位置描述date_validatedrupal/includes/form.incValidates the date type to prevent invalid dates (e.g., February 30, 2006).drupal_build_formdrupal/includes/form.incBuilds and process a form based on a form id.drupal_form_submitdrupal/includes/form.incRetrieves, populates, and processes a form.drupal_get_formdrupal/includes/form.incReturns a renderable form array for a given form ID.drupal_prepare_formdrupal/includes/form.incPrepares a structured form array.drupal_process_formdrupal/includes/form.incProcesses a form submission.drupal_rebuild_formdrupal/includes/form.incConstructs a new $form from the information in $form_state.drupal_redirect_formdrupal/includes/form.incRedirects the user to a URL after a form has been processed.drupal_retrieve_formdrupal/includes/form.incRetrieves the structured array that defines a given form.drupal_validate_formdrupal/includes/form.incValidates user-submitted form data in the $form_state array.element_validate_integerdrupal/includes/form.incForm element validation handler for integer elements.element_validate_integer_positivedrupal/includes/form.incForm element validation handler for integer elements that must be positive.element_validate_numberdrupal/includes/form.incForm element validation handler for number elements.form_builderdrupal/includes/form.incBuilds and processes all elements in the structured form array.form_clear_errordrupal/includes/form.incClears all errors against all form elements made by form_set_error().form_errordrupal/includes/form.incFlags an element as having an error.form_execute_handlersdrupal/includes/form.incExecutes custom validation and submission handlers for a given form.form_get_cachedrupal/includes/form.incFetches a form from cache.form_get_errordrupal/includes/form.incReturns the error message filed against the given form element.form_get_errorsdrupal/includes/form.incReturns an associative array of all errors.form_get_optionsdrupal/includes/form.incReturns the indexes of a select element's options matching a given key.form_load_includedrupal/includes/form.incEnsures an include file is loaded whenever the form is processed.form_options_flattendrupal/includes/form.incAllows PHP array processing of multiple select options with the same value.form_pre_render_conditional_form_elementdrupal/includes/form.incAdds form element theming to an element if its title or description is set.form_pre_render_fieldsetdrupal/includes/form.incAdds members of this group as actual elements for rendering.form_process_actionsdrupal/includes/form.incProcesses a form actions container element.form_process_autocompletedrupal/includes/form.incProcess function to prepare autocomplete data.form_process_checkboxdrupal/includes/form.incSets the #checked property of a checkbox element.form_process_checkboxesdrupal/includes/form.incProcesses a checkboxes form element.form_process_containerdrupal/includes/form.incProcesses a container element.form_process_datedrupal/includes/form.incExpands a date element into year, month, and day select elements.form_process_fieldsetdrupal/includes/form.incArranges fieldsets into groups.form_process_machine_namedrupal/includes/form.incProcesses a machine-readable name form element.form_process_password_confirmdrupal/includes/form.incExpand a password_confirm field into two text boxes.form_process_radiosdrupal/includes/form.incExpands a radios element into individual radio elements.form_process_selectdrupal/includes/form.incProcesses a select list form element.form_process_tableselectdrupal/includes/form.incCreates checkbox or radio elements to populate a tableselect table.form_process_vertical_tabsdrupal/includes/form.incCreates a group formatted as vertical tabs.form_process_weightdrupal/includes/form.incExpands a weight element into a select element.form_select_optionsdrupal/includes/form.incConverts an array of options into HTML, for use in select list form elements.form_set_cachedrupal/includes/form.incStores a form in the cache.form_set_errordrupal/includes/form.incFiles an error against a form element.form_set_valuedrupal/includes/form.incChanges submitted form values during form validation.form_state_defaultsdrupal/includes/form.incRetrieves default values for the $form_state array.form_state_keys_no_cachedrupal/includes/form.incReturns an array of $form_state keys that shouldn't be cached.form_state_values_cleandrupal/includes/form.incRemoves internal Form API elements and buttons from submitted form values.form_type_checkboxes_valuedrupal/includes/form.incDetermines the value for a checkboxes form element.form_type_checkbox_valuedrupal/includes/form.incDetermines the value for a checkbox form element.form_type_image_button_valuedrupal/includes/form.incDetermines the value for an image button form element.form_type_password_confirm_valuedrupal/includes/form.incDetermines the value for a password_confirm form element.form_type_radios_valuedrupal/includes/form.incForm value callback: Determines the value for a #type radios form element.form_type_select_valuedrupal/includes/form.incDetermines the value for a select form element.form_type_tableselect_valuedrupal/includes/form.incDetermines the value for a tableselect form element.form_type_textarea_valuedrupal/includes/form.incDetermines the value for a textarea form element.form_type_textfield_valuedrupal/includes/form.incDetermines the value for a textfield form element.form_type_token_valuedrupal/includes/form.incDetermines the value for form's token value.form_validate_machine_namedrupal/includes/form.incForm element validation handler for machine_name elements.map_monthdrupal/includes/form.incHelper function for usage with drupal_map_assoc to display month names.password_confirm_validatedrupal/includes/form.incValidates a password_confirm element.theme_buttondrupal/includes/form.incReturns HTML for a button form element.theme_checkboxdrupal/includes/form.incReturns HTML for a checkbox form element.theme_checkboxesdrupal/includes/form.incReturns HTML for a set of checkbox form elements.theme_containerdrupal/includes/form.incReturns HTML to wrap child elements in a container.theme_datedrupal/includes/form.incReturns HTML for a date selection form element.theme_fieldsetdrupal/includes/form.incReturns HTML for a fieldset form element and its children.theme_filedrupal/includes/form.incReturns HTML for a file upload form element.theme_formdrupal/includes/form.incReturns HTML for a form.theme_form_elementdrupal/includes/form.incReturns HTML for a form element.theme_form_element_labeldrupal/includes/form.incReturns HTML for a form element label and required marker.theme_form_required_markerdrupal/includes/form.incReturns HTML for a marker for required form elements.theme_hiddendrupal/includes/form.incReturns HTML for a hidden form element.theme_image_buttondrupal/includes/form.incReturns HTML for an image button form element.theme_passworddrupal/includes/form.incReturns HTML for a password form element.theme_radiodrupal/includes/form.incReturns HTML for a radio button form element.theme_radiosdrupal/includes/form.incReturns HTML for a set of radio button form elements.theme_selectdrupal/includes/form.incReturns HTML for a select form element.theme_submitdrupal/includes/form.incReturns HTML for a submit button form element.theme_tableselectdrupal/includes/form.incReturns HTML for a table with radio buttons or checkboxes.theme_textareadrupal/includes/form.incReturns HTML for a textarea form element.theme_textfielddrupal/includes/form.incReturns HTML for a textfield form element.theme_vertical_tabsdrupal/includes/form.incReturns HTML for an element's children fieldsets as vertical tabs.weight_valuedrupal/includes/form.incSets the value for a weight element, with zero as a default._drupal_invalid_token_set_form_errordrupal/includes/form.incHelper function to call form_set_error() if there is a token error._form_builder_handle_input_elementdrupal/includes/form.incAdds the #name and #value properties of an input element before rendering._form_button_was_clickeddrupal/includes/form.incDetermines if a given button triggered the form submission._form_element_triggered_scripted_submissiondrupal/includes/form.incDetects if an element triggered the form submission via Ajax._form_options_flattendrupal/includes/form.incIterates over an array and returns a flat array with duplicate keys removed._form_set_classdrupal/includes/form.incSets a form element's class attribute._form_validatedrupal/includes/form.incPerforms validation on form elements.