1

I have a field that should be visible and required if the user selects a date that would make them less than 18 years old. If they are over 18, the field should be hidden and not required. Right now, I am using this:

In birthday.js:

(function ($) {

$(document).ready(function() {
    $('#edit-field-minor-agreement').hide();
});

$(document).on('change', '#edit-field-dob-und-0-value-year, #edit-field-dob-und-0-value-month, #edit-field-dob-und-0-value-day', function() {
    var yyyy = $("#edit-field-dob-und-0-value-year").find(":selected").val();
    var m = $("#edit-field-dob-und-0-value-month").find(":selected").val();
    var d = $("#edit-field-dob-und-0-value-day").find(":selected").val();

    if ( $('#edit-field-dob-und-0-value-year').val() && $('#edit-field-dob-und-0-value-year').val() && $('#edit-field-dob-und-0-value-year').val() ){
        dateString = m + "/" + d + "/" + yyyy;
        var today = new Date();
        var birthDate = new Date(dateString);
        var age = today.getFullYear() - birthDate.getFullYear();
        var m = today.getMonth() - birthDate.getMonth();
        if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
            age--;
        }   
        console.log("Birth date: " + dateString);
        console.log("Age: " + age);
    }

    if(age<18) {
        $('#edit-field-minor-agreement').show();
    } else {
        $('#edit-field-minor-agreement').hide();
    }

});
})(jQuery);

It is successfully setting the visibility, but I can't seem to figure out how to make it required. Any help is appreciated.

  • Maybe take a peek at Conditional Fields. – prkos Oct 3 '18 at 18:12
  • I was having trouble with using a calculated value (date of birth, calculated from the date field) with Conditional Fields. – MrPeanut Oct 3 '18 at 18:40
1

Here is a working solution. Huge thanks to @nikathone for assistance.

/**
 * Implements hook_form_FORM_ID_alter() for record_node_form.
 */
function mrpeanut_form_record_node_form_alter(&$form, &$form_state, $form_id) {
  if (!empty($form['field_dob']) && !empty($form['field_minor_agreement'])) {
    $form['field_minor_agreement']['#prefix'] = '<div id="edit-field-minor-agreement-wrapper">';
    $form['field_minor_agreement']['#suffix'] = '</div>';
    $is_younger = TRUE;
    $values = !empty($form_state['values']) ? $form_state['values'] : array();
    $dob_date_combo_element = &$form['field_dob']['und'][0];
    $dob_date_combo_element['#ajax'] = [
      'callback' => 'mrpeanut_form_record_node_form_minor_agreement_callback',
      'wrapper' => 'edit-field-minor-agreement-wrapper',
      'effect' => 'fade',
    ];

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

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

/**
 * Ajax callback for minor agreement.
 */
function mrpeanut_form_record_node_form_minor_agreement_callback($form, $form_state) {
  return $form['field_minor_agreement'];
}

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.