0

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' => array(0 => array('value' => '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?

0

The code executed from form_set_value() is the following one.

function form_set_value($element, $value, &$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(&$form, &$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');.

| improve this answer | |
  • hey @kiamlaluno, I am getting the same error when I use form_set_value($form_state['values'][$checkout_pane['pane_id']]['donation_amount'], '23', $form_state); Any ideas? – Scott Lawrence Nov 28 '12 at 14:31
  • I've checked that I'm targeting the array correctly by doing ` drupal_set_message($form_state['values'][$checkout_pane['pane_id']]['donation_amount']);` and it successfully shows the original value. But still has this error when using it in form_set_value() – Scott Lawrence Nov 28 '12 at 14:37
  • I think the issue is that I'm calling this function during form validation. Another person has the same issue here: drupal.stackexchange.com/questions/17839/… No resolution for using form_set_value() to change the value though :S – Scott Lawrence Nov 28 '12 at 14:56
  • The solution is setting $element['#parents'], for what I can understand. – kiamlaluno Nov 28 '12 at 15:01
  • I have updated the question, when setting #parents it produces another error...which suggest Drupal is turning my array into a string...any thoughts? – Scott Lawrence Nov 28 '12 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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