0

I am writing a form using the Drupal 7 form API. On submitting the form (via AJAX), the following error is displayed:

An AJAX HTTP error occurred.
HTTP Result Code: 500
Debugging information follows.
Path: /en/system/ajax StatusText: Internal Server Error ResponseText: Recoverable fatal error: Argument 1 passed to drupal_array_nested_key_exists() must be an array, null given, called in /drupal7/includes/form.inc on line 1986 and defined in drupal_array_nested_key_exists() (line 6296 of /drupal7/includes/common.inc).

I have two buttons on the form (copy & submit); one is an AJAX call which does some stuff, and the other is used to save the entered data.

This is the code I'm using:

function foo_form ($form, &$form_state, $foo) {
  $form['#foo'] = $foo;
  $form_state['values'] = $foo;

  $form['#prefix'] = '<div id="foo-form-wrapper">';
  $form['#suffix'] = '</div>';

  $form['copy'] = array(
    '#type' => 'submit',
    '#value' => t('Copy foo'),
    '#limit_validation_errors' => array(),
    '#submit' => array('foo_form_submit'),
    '#ajax' => array(
      'wrapper' => 'foo-form-wrapper',
      'callback' => 'foo_form_callback',
    ),
  );

  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#required' => TRUE,
    '#maxlength' => 255,
    '#default_value' => (!isset($form_state['values']['name']) ? $foo->name : $form_state['values']['name']),
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );

  return $form;
}

function foo_form_callback($form, $form_state) {
  return $form;
}

function foo_form_submit ($form, &$form_state) {
  // Did we use the AJAX Call?
  // @todo: can we check this some other way?
  if (!isset($form_state['triggering_element']['#ajax'])) {

    // We didn't, save the entered values

    $form_state['rebuild'] = false;
    return;
  } else {

    // We did, do some behind the scenes stuff...

    $form_state['rebuild'] = true;
  }
}

Does anyone have an idea of what could be wrong?

3

This line is wrong:

$form_state['values'] = $foo;

No idea what you are trying to do here, but you must not mess with $form_state['values'] directly.

And what you are trying to do with the #default_value shouldn't be necessary either. Form API should automatically re-use an already entered value, without you having to set it as the default_value.

  • Concerning the default value: should be (!isset($form_state['values']['name']) ? $foo->name : $form_state['values']['name']), I'll edit the post. – Bart May 12 '11 at 16:53
  • 1
    Yes, and exactly that shouldn't be necessary. Simply use #default_value' => $foo->name – Berdir May 12 '11 at 17:06
  • I have removed the '$form_state['values'] = $foo;' and edited the default values. The error now is gone but when I press the "copy" button the page reloads. Also, in what method am I supposed to change what's in the "name" field? – Bart May 13 '11 at 7:22
  • form_set_value() should help you. Also, the limit validation errors thing looks wrong... – Berdir May 13 '11 at 7:24
  • The limit validation errors is used because when I push the "copy" button, the validation triggers and errors are displayed. I got this from the examples module (examples/ajax_example/wizard). What I am trying to do is change the "name" field when I press "Copy". – Bart May 13 '11 at 7:31

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.