Error using form API on AJAX submit 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 &amp; 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, &amp;$form_state, $foo) {
  $form['#foo'] = $foo;
  $form_state['values'] = $foo;

  $form['#prefix'] = '&lt;div id="foo-form-wrapper"&gt;';
  $form['#suffix'] = '&lt;/div&gt;';

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

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

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

  return $form;
}

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

function foo_form_submit ($form, &amp;$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?
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.