function form_set_value

You are here

7 form.inc form_set_value($element, $value, &$form_state)
4.7 form.inc form_set_value($form, $value)
5 form.inc form_set_value($form, $value)
6 form.inc form_set_value($form_item, $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()
field_test_widget_multiple_validate in modules/field/tests/field_test.field.inc
Form element validation handler for 'test_field_widget_multiple' widget.
file_managed_file_submit in modules/file/file.module
Form submission handler for upload / remove buttons of managed_file elements.
file_managed_file_validate in modules/file/file.module
An #element_validate callback for the managed_file element.
filter_admin_format_form_validate in modules/filter/filter.admin.inc
Form validation handler for filter_admin_format_form().
form_test_element_validate_name in modules/simpletest/tests/form_test.module
Form element validation handler for 'name' in form_test_validate_form().

... See full list

File

includes/form.inc, line 2566
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);
}

Comments

Using

<?php
form_set_value
($element, $value, &$form_state)
?>

in a custom module, provoked this "error" to appear :
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 :

<?php
form_set_value
($element, $value, $form_state)
?>

Hopes that helps others.

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 put my_function($variable = NULL) in your code. The '= NULL' tells you the variable is optional, meaning you can call my_function($variable) or my_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.

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 the LANGUAGE_NONE constant rather than 'und'. http://api.drupal.org/api/drupal/includes--bootstrap.inc/constant/LANGUA...

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 :

<?php
form_set_value
($form['field_event_title'], array(0 => array('value' => $newvalue)), $form_state);
?>

I don't understand array(0 => array('value' => $newvalue))
Can you explain me what I must put there.

thanks.

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.

Confirmed !
Here are both ways (without the missing ' in thissideup's example) and with what is required:

<?php
$form
['field_your_field']['#parents'] = array('field_your_field');
form_set_value($form['field_your_field'], array('und' => array(0 => array('value' => 'WORKS'))), $form_state);
?>
<?php
$form
['field_your_field']['#parents'] = array('field_your_field');
form_set_value($form['field_your_field'], array(LANGUAGE_NONE => array(0 => array('value' => 'WORKS'))), $form_state);
?>

Double-confirmed. At long last. Thanks punkrack

Thanks!!!

This function wasn't working that well and I've used following code:

function hook_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'my_form'){
  // put as first my custom submission function
    array_unshift($form['#submit'], 'my_form_submit_custom' );
  }

}

function my_form_submit_custom($form, &$form_state){
  $form_state['values']['field_to_change'][LANGUAGE_NONE][0]['value'] = $new_value;
}

I've tried your solution and it doesn't work...

This works for me:

function my_form_submit_custom($form, &$form_state){
  $form_state['values']['field_to_change'][LANGUAGE_NONE][0]['value'] = $new_value;
}

Howdy All!

I'm trying to make sense of the form_set_value function and the parameters it requires.

I need to be able to establish a node reference to a node that I creat during the custom validation handle, which I've enabled via hook_form_alter.

During the custom validation handler the referenced node is successfully created and all elements are being populated but try as I might I cant figure out how to update the form_state array with the nid of the newly created node.

          node_save($node);
          $form['field_existing_providers']['#parents'] = array(LANGUAGE_NONE,0,'nid');

          form_set_value($form['field_existing_providers'], $node->nid, $form_state);

Any thoughts would be appreciated
//form_set_value($form_state['values']['field_existing_providers']['und'][0]['nid'],$node->nid, $form_state);

Here is some more documentation for those who want it!

https://drupal.org/node/160160#comment-820709
https://drupal.org/node/51104

For those who would like to update value of field collection item,
use this:
$form_state['values'][*field collection name*]['und'][*field item id*]['entity']->*field item name*['und'][0['value'] = your value;

Thanks! That was losing me ages trying to figure out...

As encoding the counts and the language and the field names into that great long array is dangerous in the general case, try this also:

<?php
 
// Given $element, $your_field_name and $your_field_value...
 
  // Retrieve the field_collection element values from the form submission.
 
$element_values = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
 
 
// This object handle is special to field_collection and what we need to change.
 
$entity_handle = $element_values['entity'];
 
 
// Prepare the value that needs to be saved in a special structure.
 
$entity_field_value = array(
     
$element['#language'] => array(
       
0 => array(
         
'value' => $your_value,
        )
      )
  );
 
$entity_handle->$your_field_name = $entity_field_value;
?>