Drupal ---- confused with $ form_state argument

This is an example of a pro drupal book form.

function annotate_admin_settings() { $options = node_get_types('names'); $form['annotate_node_types'] = array( '#type' => 'checkboxes', '#title' => t('Users may annotate these content types'), '#options' => $options, '#default_value' => variable_get('annotate_node_types', array('page')), '#description' => t('A text field will be available on these content types to make user-specific notes.'), ); return system_settings_form($form); } 

but create the drupal documentation, the form style creator looks like this.

 function mymodule_myform($form_state) { } 

there is a parameter in the function ($ form_state) when I should use this parameter. Thanks.

+4
source share
3 answers

thosewhatnots is completely perfectly correct when discussing the difference between $form and $form_state . $form defines the form, $form_state carries information about the processed form.

So, why do form-building functions accept $form_state as their first argument (along with any number of optional user-defined arguments)? Provide information about the state and condition that may be needed when creating the form.

Form building functions should always return a form definition , but they can make decisions about form definition based on information such as the last button clicked, user values, or other information. As mentioned in ceejayoz's comments, the general use of $form_state in the builder function is to handle multi-stage forms in which the state of the form ("step 1 + 2 is complete, step 3 should still go") has an effect in the form displayed to the user.

In many common use cases, $form_state completely ignored, and optional arguments are used to provide contextual information (for one of many examples, see comment_form ).

+2
source

If you are looking for differences between $ form and $ form_state:

$ form is an associative array containing the structure of the form. It is modified with calls like hook_form_alter to add fields or do other things.

$ form_state is a key array containing the current state of the form. The exact state depends on what stage of the node process the form is in. Typically, it contains values ​​that will be processed when the form is submitted.

+4
source

The main use of $form_state is to hold the values ​​of the form after it has been completed and submitted. $form_state validated by validation functions to ensure that the values ​​entered match the validation requirements. Then the same $form_state sent to send functions that can perform actions with this data after passing the test (for example, save data to the database, print something on the next page, etc.).

However, the $form_state parameter can be included in any form function because the form can be loaded or reloaded at any time during the submit process. If you need access to the values ​​stored in your form, make sure $form_state is available to your function.

See Drupal Form Quickstart for more information , especially the validation and submit sections.

+2
source

All Articles