| 5 form.inc | form_set_value( |
| 6 form.inc | form_set_value( |
| 7 form.inc | form_set_value($element, $value, &$form_state) |
| 8 form.inc | form_set_value($element, $value, &$form_state) |
Changes submitted form values during form validation.
Use this function to change the submitted value of a form element in a form validation function, so that the changed value persists in $form_state through the remaining validation and submission handlers. It does not change the value in $element['#value'], only in $form_state['values'], which is where submitted values are always stored.
Note that form validation functions are specified in the '#validate' component of the form array (the value of $form['#validate'] is an array of validation function names). If the form does not originate in your module, you can implement hook_form_FORM_ID_alter() to add a validation function to $form['#validate'].
Parameters
$element: The form element that should have its value updated; in most cases you can just pass in the element from the $form array, although the only component that is actually used is '#parents'. If constructing yourself, set $element['#parents'] to be an array giving the path through the form array's keys to the element whose value you want to update. For instance, if you want to update the value of $form['elem1']['elem2'], which should be stored in $form_state['values']['elem1']['elem2'], you would set $element['#parents'] = array('elem1','elem2').
$value: The new value for the form element.
$form_state: Form state array where the value change should be recorded.
Related topics
24 calls to form_set_value()
File
- includes/
form.inc, line 2499 - Functions for form and batch generation and processing.
Code
function form_set_value($element, $value, &$form_state) {
drupal_array_set_nested_value($form_state['values'], $element['#parents'], $value, TRUE);
}
Login or register to post comments
Comments
Call-time pass-by-reference has been deprecated
Using
<?phpform_set_value($element, $value, &$form_state)
?>
Deprecated function: Call-time pass-by-reference has been deprecated in drupal_load() (line 911 of .../includes/bootstrap.inc).So I had to delete the "&" of the function to end with this message, like this :
<?phpform_set_value($element, $value, $form_state)
?>
Hopes that helps others.
Docs
Yeah, the variables listed throughout the documentation tend to refer to the way the function is declared rather than the way it should be called - you'll see some with default variables, eg
function my_function($variable = NULL). It generally tells you a little about what types of variables the function will expect, and how it will behave - for example, you shouldn't putmy_function($variable = NULL)in your code. The '= NULL' tells you the variable is optional, meaning you can callmy_function($variable)ormy_function().In the case of form_set_value(), the & tells you that the $form_state variable will be modified directly within the function, rather than cloned and returned.
the $element and the $value
since I had a hard time figuring what the description meant, let me give an example:
I had a field called 'field_event_title', which has it's value stored in
$form['field_event_title']['und'][0]['value']['#value']which means it is also stored in$form_state['value']['field_event_title']['und'][0](note that 'und' refers to the language of the value, and 0 refers to the delta (i think..)). now to update this value, simply write:form_set_value($form['field_event_title'],array('und => array(0 => array('value' => $newvalue))),$form_state);I hope that helps anyone.
It's better practice to use
It's better practice to use the LANGUAGE_NONE constant rather than 'und'. http://api.drupal.org/api/drupal/includes--bootstrap.inc/constant/LANGUAGE_NONE/7
Not working with D7.12
I do not know if you made a mistake or if drupal behavior changed but your sample do not work under Drupal 7.12
In fact, form_set_value already handle the language so you just have to write :
<?phpform_set_value($form['field_event_title'], array(0 => array('value' => $newvalue)), $form_state);
?>
I don't understand array(0 =>
I don't understand array(0 => array('value' => $newvalue))
Can you explain me what I must put there.
thanks.
thissideup's and friedhof's
thissideup's and friedhof's works in my 7.12 installation.
(languages en, pt-br included, only pt-br enabled).
DuaelFr's suggestion did not work.
Thank you guys! I was lost before find your comments.