AJAX added form element disappears after failed validation? I have a form field which uses the #ajax element. When a user interacts with this field a function is called and a new form element is added to the form and then the new form is displayed to the user.

However, if validation fails the added element disappears? Of course, I want it to remain.

How do I solve this problem?
Generally it's not good to create the elements in the ajax callback. What I usually do is add the form elements to the main form, but wrap them in the if statement, and then in the ajax callback just refresh the container of that element or the whole form if it's small.

function SOMETHING_form($form, &amp;$form_state) {

  $form['nominee'] =&gt; array(
    '#type' =&gt; 'checkbox', 
    '#title' =&gt; t('I am nominating someone else'),
    '#ajax' =&gt; array(
      'callback' =&gt; 'return_nominee_email_element',
      'wrapper' =&gt; 'nominee-email-wrapper',
      'progress' =&gt; array('type' =&gt; 'none'),
    ),
  ),

  if(isset($form_state['values']) &amp;&amp; $form_state['values']['contact_details']['nominee'] == TRUE) {
    $form['nominee_email'] = array(
      '#type' =&gt; 'textfield',
      '#title' =&gt; t('Email'),
      '#required' =&gt; TRUE,
      '#prefix' =&gt; '&lt;div id="nominee-email-wrapper"&gt;',
      '#suffix' =&gt; '&lt;/div&gt;',
    );
  } else {
    $form['nominee_email'] = array(
      '#prefix' =&gt; '&lt;div id="nominee-email-wrapper"&gt;',
      '#suffix' =&gt; '&lt;/div&gt;',
    );
  }

 return $form; 
}

function return_nominee_email_element($form, &amp;$form_state) {
  return $form;
}


See if something like that works for you.