Using form_set_value with commerce in custom form I'm trying to change the value of a form within a custom Commerce Pane during validation but I get the following error:


  Recoverable fatal error: Argument 2 passed to drupal_array_set_nested_value() must be an array, string given, called in /var/www/vhosts/innovista.org/httpdocs/includes/form.inc on line 2514 and defined in drupal_array_set_nested_value() (line 6440 of /var/www/vhosts/innovista.org/httpdocs/includes/common.inc).


As far as I see my code is an array.

form_set_value($form_state['values'][$checkout_pane['pane_id']]['donation_amount'], array('und' =&gt; array(0 =&gt; array('value' =&gt; '999'))), $form_state);


I've been using $checkout_pane['pane_id'] . '][donation_amount' with form_set_error(), and it works fine. I can't understand why argument 2 is not working.

I have tried setting #parents as this question suggests:
Using form_set_value() in Drupal 7

In the following way:

$form_state['values'][$checkout_pane['pane_id']]['donation_amount']['#parents'] = array( 'values',$checkout_pane['pane_id'], 'donation_amount');


But this produces an extra error:


  Notice: Array to string conversion in
  donation_checkout_pane_pane_checkout_form_validate() (line 186 of
  /var/www/vhosts/innovista.org/httpdocs/sites/all/modules/donation_checkout_pane/donation_checkout_pane.module).


Do you have any idea?
The code executed from form_set_value() is the following one.

function form_set_value($element, $value, &amp;$form_state) {
  drupal_array_set_nested_value($form_state['values'], $element['#parents'], $value, TRUE);
}


$checkout_pane['pane_id'] . '][donation_amount' is a string, even if $checkout_pane['pane_id'] is an array; this is because the string concatenation operator cast an array into a string.
If $element is a string, then $element['#parents'] is a string made of one character, similarly to what happens with $string[0], when $string is set to 'long string with many characters that are added just to make the string longer'.

That explains the error about the second argument.

With form_set_error(), $checkout_pane['pane_id'] . '][donation_amount' works because the function is expecting a string as first argument, and the format of that string is exactly the format of the string you are passing. 

The first argument of form_set_value() is a form API array for the form element for which you want to set the value. That is not the content of $form_state['values']. As that is the code for a form validation handler, it should be similar to the following one.

function mymodule_form_validate(&amp;$form, &amp;$form_state) {
  // …
  $form[$checkout_pane['pane_id']]['donation_amount']['#parents'] = array($checkout_pane['pane_id'], 'donation_amount');
  form_set_value($form[$checkout_pane['pane_id']]['donation_amount'], '999', $form_state);
}


You still need to verify the content of $checkout_pane['pane_id'] is effectively the parent of the element you want to change. For that, you can install the Devel module, and print the structure of $form using dpm($form);.

As for the answer given in the other question, I can say it is based on the description present in form_set_value() for the $element parameter.


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