0

Drupal 7. in my validate form function, I use form_set_value to set the value for the field "statut_metier" to 'draft'

If my field is a text field, this code works :

form_set_value($form['field_statut_metier'][LANGUAGE_NONE][0]['value'], 'draft', $form_state);

If my field is a list(text) field, with the key for the value 'draft' = 5

I can't manage to have the form_set_value function work.

Using Devel and dpm() I have search to reach this field value :

I have tried :

form_set_value($form['field_statut_metier2'][LANGUAGE_NONE]['#value'][0], '5' , $form_state);

but it doesn't work.

Any idea ?

2
  • See if this answer helps. Feb 22, 2016 at 13:18
  • Thanks for helping, I read this post but it doesn't seem to be related to select list fields. Feb 22, 2016 at 13:53

1 Answer 1

1
<?php form_set_value($form['field_statut_metier2'][LANGUAGE_NONE], array(0 => array('value' => 2)), $form_state); ?>

From the form_set_value() documentation on the first argument:

you can just pass in the element from the $form array, although the only component that is actually used is '#parents'

Alternatively you can do:

<?php $form_state['values']['field_statut_metier2'][LANGUAGE_NONE][0]['value'] = '2'; ?>

... which is what form_set_value() does anyways.

1
  • 1
    Thanks, it works great, using this code : form_set_value($form['field_statut_metier'][LANGUAGE_NONE], array(0 => array('value' => 5)), $form_state); I don't know why the field value is an array (it's not a multi value field), but know I know how to use this function when the value is an array. Feb 22, 2016 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.