0

I am trying to disable a date field's second value, inside a field collection, but it isn't working.

My hook_form_alter has this:

  $form['field_certification']['und'][0]['field_validity']['und'][0]['#states'] = array(
        'disabled' => array(
          'input[name="field_certification[und][0][field_auto_calculate_validity][und]"]' => array('checked' => TRUE),
        ),
      );

The HTML for the date field is here:

<input class="date-clear form-text hasDatepicker date-popup-init" type="text" id="edit-field-certification-und-0-field-validity-und-0-value2-datepicker-popup-0" name="field_certification[und][0][field_validity][und][0][value2][date]" value="07/23/2014" size="20" maxlength="30">

The checkbox is here:

<input type="checkbox" id="edit-field-certification-und-0-field-auto-calculate-validity-und" name="field_certification[und][0][field_auto_calculate_validity][und]" value="1" class="form-checkbox">

Could it be because I'm not specifically selecting the (second) value of the date field?

  • By 'disable a date field collection field's second value', do you mean to prevent the display of the second form element within that collection? – autopoietic Dec 11 '14 at 16:30
  • I have a field collection, and I have a date field inside of that field collection. That date field has both start and end dates (or to and from dates, whatever you want to call them). I want to disable the second one when a given checkbox is checked. – Jack Ryan Dec 11 '14 at 16:39
  • I missed the relevance of the checkbox. I guess you would need to do this with javascript/jquery (adding a click event to the checkbox to disable the field), though maybe Drupal's AHAH functionality could be of use: drupal.org/node/331941 – autopoietic Dec 11 '14 at 17:13
  • updated solution, hope helps – autopoietic Dec 11 '14 at 17:19
0

My original answer totally missed the point (apologies), this is updated version:

I do not think you can use Drupal's form api #state property to accomplish what you would like. States can be used to hide, reveal and disable whole form elements, such as a fieldset, text input or date field, but you aim to disable one part of a single (contrib) element (ie the second value of a date_combo field provided by the date module).

There are two possible solutions:

  • load your own jquery when the form is loaded, and add a change() function to disable the relevant date field component
  • separate the start/end dates into separate date fields and use the drupal #states functionality (as above) to disable the (whole) second date field:

    $form['field_certification']['und'][0]['field_validity']['#states'] = array(
    'disabled' => array(
        ':input[name="field_certification[und][0][field_auto_calculate_validity][und]"]' => array(
            'checked' => true
        ),
    )
    

    );

I hope this helps, I played around with it on a blank d7 install, and this was the only way I could get your intended functionality to work.

  • apologies for repeated updates – autopoietic Dec 11 '14 at 17:30
  • No, I can get it to disable the date field generally, but I want only the second date value to be grayed out. And yes, I corrected the lack of : in my code. – Jack Ryan Dec 11 '14 at 18:15
  • I will try to replicate your issue. – autopoietic Dec 12 '14 at 8:07

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.