Skip to main content
2 of 5
added 221 characters in body
alex
  • 373
  • 1
  • 4
  • 14

drupal7: using form_set_value()

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 30 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!

[EDIT]

My complete function is:

    function hook_node_validate($node,$form,&$form_state) {
      if(fancy_stuff){
        form_set_value();
        form_set_error();
      } 
    }
alex
  • 373
  • 1
  • 4
  • 14