form_set_value

Versions
4.7 – 5
form_set_value($form, $value)
6
form_set_value($form_item, $value, &$form_state)
7
form_set_value($element, $value, &$form_state)

Change submitted form values during the form processing cycle.

Use this function to change the submitted value of a form item in the validation phase so that it persists in $form_state through to the submission handlers in the submission phase.

Since $form_state['values'] can either be a flat array of values, or a tree of nested values, some care must be taken when using this function. Specifically, $element['#parents'] is an array that describes the branch of the tree whose value should be updated. For example, if we wanted to update $form_state['values']['one']['two'] to 'new value', we'd pass in $element['#parents'] = array('one', 'two') and $value = 'new value'.

Parameters

$element The form item that should have its value updated. Keys used: #parents, #value. In most cases you can just pass in the right element from the $form array.

$value The new value for the form item.

$form_state The array where the value change should be recorded.

Related topics

▾ 16 functions call form_set_value()

field_ui_field_edit_form_validate in modules/field_ui/field_ui.admin.inc
Validate a field's settings.
file_managed_file_validate in modules/file/file.module
An #element_validate callback for the managed_file element.
install_settings_form_validate in ./install.php
Form API validate for install_settings form.
node_search_validate in modules/node/node.module
Form API callback for the search form. Registered in node_form_alter().
number_field_widget_validate in modules/field/modules/number/number.module
FAPI validation of an individual number element.
options_field_widget_validate in modules/field/modules/options/options.module
Form element validation handler for options element.
password_confirm_validate in includes/form.inc
Validate password_confirm element.
path_form_element_validate in modules/path/path.module
Form element validation handler for URL alias form element.
search_form_validate in modules/search/search.pages.inc
As the search form collates keys from other modules hooked in via hook_form_alter, the validation takes place in _submit. search_form_validate() is used solely to set the 'processed_keys' form value for the basic search form.
taxonomy_autocomplete_validate in modules/taxonomy/taxonomy.module
Form element validate handler for taxonomy term autocomplete element.
user_pass_validate in modules/user/user.pages.inc
_field_ui_field_overview_form_validate_add_new in modules/field_ui/field_ui.admin.inc
Helper function for field_ui_field_overview_form_validate.
_file_generic_settings_extensions in modules/file/file.field.inc
Element validate callback for the allowed file extensions field.
_file_generic_settings_file_directory_validate in modules/file/file.field.inc
Element validate callback for the file destination field.
_form_builder_handle_input_element in includes/form.inc
Populate the #value and #name properties of input elements so they can be processed and rendered.
_image_field_resolution_validate in modules/image/image.field.inc
Element validate function for resolution fields.

Code

includes/form.inc, line 1575

<?php
function form_set_value($element, $value, &$form_state) {
  _form_set_value($form_state['values'], $element, $element['#parents'], $value);
}
?>
Login or register to post comments
 
 

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.