I am trying to update a value during the validate phase of a node-form, i.e. if the custom validation error is fired, just empty one of the fields.

for the last 60 hours I am trying to make sense of the drupal api, but I am giving up. I just do not seem to get the idea what the different values mean.

the function is: form_set_value($element, $value, &$form_state)

now i understand that the last value is simply the $form_state, that I am having through the validate function. but what about $element and $value?

I was trying a lot and apparently the desired value resides in $form['field_name']['und'][0]['value']['#value'] and only there.

but when I am trying form_set_value($form['field_name']['und'][0]['value']['#value'],'foo',$form_state) it raises

Recoverable fatal error: Argument 2 passed to drupal_array_set_nested_value() must be an array, string given, called in /includes/form.inc on line 2436 and defined in drupal_array_set_nested_value()

and when I am trying:

$newvalue = $form['field_name']['und'][0]['value'];
$newvalue['#value']='foo';
form_set_value($form['field_name']['und'][0]['value'],$newvalue,$form_state);

it raises:

Warning: mb_strlen() expects parameter 1 to be string, array given in drupal_strlen()

Thanks for any help!

P.S. my function currently looks as this:

//calls when node is being created
  function eventcheck_node_validate($node,$form,&$form_state) {
    //checks if the node type is applicable
    if($node -> type=='article') {
      //checks if field 'field_event_choose' AND either 'field_event_title' 
      //or 'field_event' is supplied, if so, fire form_set_value() and form_set_error()
      if ((array_key_exists('endpoints',$node -> field_event_choose['und'][0])) && 
      (($node -> field_event_title['und'][0]['value']<>'') || (isset($node -> 
      field_event['und'][0]['value'])))) {

        //fire away
        $form_set_value(???);
        $form_set_error('event_checker_group',t('sorry blabla'));

      }
    }
  }

Comments

thissideup’s picture

After a lot of debugging, I finally managed to make this work. The trick lies inside $form['complete form']. but first things first, how does form_set_value() work and what does it do?

the `form_set_value() function

as the docs suggested:

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').

now what does that mean? in my case, I had a textfield called 'field_event_title', which is the name I gave it on creation. in $form, all fields have a sub-array in $form['field_name'], which is in my case $form['field_event_title']. this is where the submitted value also is stored. now since it is a textfield, drupal maintains both the language and the delta [question for editors: is this right?] of the inputted data. so in fact, the value is not stored in $form['field_name']['value'], but in $form['field_name']['und'][0]['value'] (['und']=language; [0]=delta). note that 'und' is the drupal key for the default language of the site, if it is, say, in german, then it would be 'de', however, in most cases it should be 'und'.

to actually change the value using form_set_value(), one is ought to invoke the function by writing:
form_set_value($form['field_name'],array('und' => array(0 => array('value' => 'foo'))),$form_state);

i.e. $element = $form['field_name'] $value=array('und' => array(0 => array('value' => 'foo')))

updating a form to repopulate it with different values than submitted (or clearing them)

but that did not work in my case since I wanted to clear fields once a custom validation error has been invoked. now one would suspect that the form repopulates itself using the values inside $form_state['values'] (which is actually the place where the values are stored, the actual place that gets updated when using form_set_value() and the place which generates the $form later.), but that is not the case: it uses the values inside $form_state['complete form'], which is a 'copy' of $form (notice that it is spelled 'complete form', with a space, not an underscore).

so using $form_state['complete form']['field_name']['und'][0]['value']['#value']='foo'; is what updates the values that actually repopulate the form on a validation error. (note: you can, as do I in my usecase, set it to =NULL to simply empty the field).

summary

now where is the difference between $form['field_name'] (e.g. updating through form_set_value()) and $form['complete form']? well, the former updates the actual value, which then gets stored inside the database, the latter is being used to repopulate a form when it failed a validation.

Elijah Lynn’s picture

-----------------------------------------------
The Future is Open!

Elijah Lynn’s picture

More documentation for anyone coming from the internets:

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

-----------------------------------------------
The Future is Open!

monaw’s picture

> now where is the difference between $form['field_name'] (e.g. updating through form_set_value()) and $form['complete form']?

I presume you mean $form_state['complete form'] and not $form['complete form'] which does not exist.

> so using $form_state['complete form']['field_name']['und'][0]['value']['#value']='foo'; is what updates the values that actually repopulate the form on a validation error. (note: you can, as do I in my usecase, set it to =NULL to simply empty the field).

I tried to clear my reference field but it didn't work:

$form_state['complete form']['field_user_data_access_users'][LANGUAGE_NONE][0]['target_id'] = NULL;

Help!

gaellafond’s picture

@thissideup Thank you so much! I was pulling my hair out trying to use the API. That form_set_value() function is not as useful as it should be. Every time I try to use it, I run into some "special case" that makes it inappropriate.

I would like to add:
If you try to modify the value on the node after a successful submit, you have to use:
form_set_value($form['field_name'], array(LANGUAGE_NONE => array(0 => array('value' => 'foo'))), $form_state);
The documentation is not clear related to the format of the "value".

To avoid inconsistent behaviour, I suggest to do both:
Show the value in the form (in case the form submit fail) - this is really missing out from form_set_value() function
$form_state['complete form']['field_name'][LANGUAGE_NONE][0]['value']['#value'] = 'foo';
AND modify the value on the node itself after submit
form_set_value($form['field_name'], 'foo', $form_state);
So the user sees what will be submit.