Image field causes #access to reset to default I am using ajax to show/hide a field based on a date field. I set the default access to TRUE. If the user selects a date of birth over 18 years old, $is_younger and consequently the access switch to FALSE.

function mrpeanut_form_record_node_form_alter(&amp;$form, &amp;$form_state, $form_id) {
    // some other stuff here
    $is_younger = TRUE;
    $values = !empty($form_state['values']) ? $form_state['values'] : array();
    $dob_date_combo_element = &amp;$form['field_dob']['und'][0];
    $dob_date_combo_element['#ajax'] = [
      'callback' =&gt; 'mrpeanut_form_record_node_form_minor_agreement_callback',
      'wrapper' =&gt; 'minor-ajax',
      'effect' =&gt; 'fade',
    ];

    if (!empty($values['field_dob']['und'][0]['value'])) {
      $dob = new DateTime($values['field_dob']['und'][0]['value']);
      $now = new DateTime();
      $age = $now-&gt;diff($dob)-&gt;y;
      $is_younger = $age &lt; 18;
    }

    $form['field_minor_agreement']['und']['#access'] = $is_younger;


This is working great until the user selects and uploads an image on a separate image field. Then, $is_younger switches back to TRUE, regardless of whether they are over 18 or not. I can't seem to figure out why.

I think the error is happening on this line:

if (!empty($values['field_dob']['und'][0]['value']))


After I upload an image, I get this error if I try to watchdog the value.


  Notice: Undefined index: field_dob in
  mrpeanut_form_record_node_form_alter()


I think $values is getting overwritten after the image is uploaded and therefore date of birth can't be calculated again and it reverts back to $is_younger = TRUE.

Here's my callback:

function mrpeanut_form_record_node_form_minor_agreement_callback($form, $form_state) {
  $commands = array();
  $commands[] = ajax_command_replace('#edit-field-minor-agreement-wrapper', drupal_render($form['field_minor_agreement']));
  return array('#type' =&gt; 'ajax', '#commands' =&gt; $commands);
}
I would try refactoring your code, so that the part that produces the value of $is_younger first checks whether the triggering element is the field_dob field.

As an example:

// some other stuff here
$dob_date_combo_element = &amp;$form['field_dob']['und'][0];
$dob_date_combo_element['#ajax'] = [
  'callback' =&gt; 'mrpeanut_form_record_node_form_minor_agreement_callback',
  'wrapper' =&gt; 'minor-ajax',
  'effect' =&gt; 'fade',
];

// Were we called by the #ajax-ified element?
if (isset($form_state['triggering_element']) &amp;&amp; 
  $form_state['triggering_element']['#name'] === 'field_dob[und][0][value]') {
  $values = !empty($form_state['values']) ? $form_state['values'] : array();
  $is_younger = TRUE;

  if (!empty($values['field_dob']['und'][0]['value'])) {
      $dob = new DateTime($values['field_dob']['und'][0]['value']);
      $now = new DateTime();
      $age = $now-&gt;diff($dob)-&gt;y;
      $is_younger = $age &lt; 18;
  }
  $form['field_minor_agreement']['und']['#access'] = $is_younger;
}
// Initial form loading.
else if (empty($form_state['input']) {
  // Place initial loading logic here for field_minor_agreement.
}