0

I'm trying to hide/disable field when the user select an option (Example at images).

Image A enter image description here Image B enter image description here

I've tried to used AJAX, JS or #states from Drupal 7, and it works, but when the user press "Submit" and the "Validate" method throws a "form set error", the selected item does not correspond to the status of the fields (Disable/Hide). It takes the default values from $form element definition. Is there any way to do it? Thanks for comment and help!

PD: Ajax callback is associated to the radio group and it change the field states.

Form:

// Schedule fieldset.
$form['schedule'] = [
  '#prefix' => '<div class="booking-fieldset-wrapper">',
  '#suffix' => '</div>',
  '#title'  => t('Schedule'),
  '#type'   => 'fieldset',
  '#weight' => $weight++,
];
// Schedule pattern type.
$form['schedule']['pattern_type'] = [
  '#prefix'        => '<div class="row"><div class="col-md-5">',
  '#suffix'        => '</div>',
  '#ajax'          => [
    'callback' => 'w_tpgal_services_bookings_schedule_callback',
  ],
  '#type'          => 'radios',
  '#title'         => t('Select the requested type of schedule'),
  '#options'       => [
    BookingDTO::EVERY_X_DAYS    => t('Every X days (Daily)'),
    BookingDTO::EVERY_X_WEEKS   => t('Every X weeks (Weekly)'),
    BookingDTO::PICK_DATE_RANGE => t('Pick date range'),
  ],
  '#required'      => TRUE,
  '#default_value' => !empty($form_state['values']) && !empty($form_state['values']['pattern_type']) ? $form_state['values']['pattern_type'] : BookingDTO::EVERY_X_DAYS,
];
// Frequency.
$form['schedule']['options']['frequency'] = [
  '#attributes'    => [
    'class' => ['js-select2'],
  ],
  '#prefix'        => '<div class="row"><div class="col-md-6">',
  '#suffix'        => '</div>',
  '#options'       => ['' => t('-- Select --')] + $freq_options,
  '#default_value' => '',
  '#type'          => 'select',
  '#title'         => t('Frequency') . ' ' . theme_form_required_marker([]),
  //      '#states'        => [
  //        'disabled' => [
  //          ':input[name="pattern_type"]' => ['value' => BookingDTO::PICK_DATE_RANGE],
  //        ],
  //        'enabled'  => [
  //          ':input[name="pattern_type"]' => ['!value' => ''],
  //        ],
  //      ],
];
// Pattern days.
$form['schedule']['options']['pattern_days'] = [
  '#attributes' => [
    'class' => ['js-select2'],
  ],
  '#prefix'     => '<div class="col-md-6">',
  '#suffix'     => '</div></div></div></div>',
  '#type'       => 'select',
  '#title'      => t('Day') . ' ' . theme_form_required_marker([]),
  '#options'    => [
    ''                        => t('-- Select --'),
    BookingDTO::DAY_MONDAY    => t('Monday'),
    BookingDTO::DAY_TUESDAY   => t('Tuesday'),
    BookingDTO::DAY_WEDNESDAY => t('Wednesday'),
    BookingDTO::DAY_THURSDAY  => t('Thursday'),
    BookingDTO::DAY_FRIDAY    => t('Friday'),
    BookingDTO::DAY_SATURDAY  => t('Saturday'),
    BookingDTO::DAY_SUNDAY    => t('Sunday'),
  ],
  //      '#states'     => [
  //        'enabled'  => [
  //          ':input[name="pattern_type"]' => ['value' => BookingDTO::EVERY_X_WEEKS],
  //        ],
  //        'disabled' => [
  //          ':input[name="pattern_type"]' => ['!value' => ''],
  //        ],
  //      ],
];

Ajax:

function w_tpgal_services_bookings_schedule_callback($form, &$form_state) {
  $commands = [];

  // Get selected option.
  if (!empty($form_state['values']['pattern_type'])) {
    $option = $form_state['values']['pattern_type'];

    switch ($option) {
      case BookingDTO::EVERY_X_DAYS:
        $form['schedule']['options']['pattern_days']['#attributes']['disabled'] = TRUE;
        unset($form['schedule']['options']['frequency']['#attributes']['disabled']);
        break;

      case BookingDTO::EVERY_X_WEEKS:
        unset($form['schedule']['options']['pattern_days']['#attributes']['disabled']);
        unset($form['schedule']['options']['frequency']['#attributes']['disabled']);
        break;

      case BookingDTO::PICK_DATE_RANGE:
        $form['schedule']['options']['pattern_days']['#attributes']['disabled'] = TRUE;
        $form['schedule']['options']['frequency']['#attributes']['disabled']    = TRUE;
        break;
    }
  }
  $commands[] = ajax_command_replace('.schedule-fieldset-wrapper', render($form['schedule']['options']));

  return ['#type' => 'ajax', '#commands' => $commands];
}
1
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Clive
    Commented Aug 17, 2018 at 11:46

1 Answer 1

0

Disabled fields do not get submitted in form submit. If you want to mandate field's based on the select list of "pattern_type" use below code with required in #states.:

// Schedule fieldset.
  $form['schedule'] = [
    '#prefix' => '<div class="booking-fieldset-wrapper">',
    '#suffix' => '</div>',
    '#title' => t('Schedule'),
    '#type' => 'fieldset',
    '#weight' => $weight++,
  ];
// Schedule pattern type.
  $form['schedule']['pattern_type'] = [
    '#type' => 'radios',
    '#title' => t('Select the requested type of schedule'),
    '#options' => [
      BookingDTO::EVERY_X_DAYS => t('Every X days (Daily)'),
      BookingDTO::EVERY_X_WEEKS => t('Every X weeks (Weekly)'),
      BookingDTO::PICK_DATE_RANGE => t('Pick date range'),
    ],
    '#required' => TRUE,
    '#default_value' => !empty($form_state['values']) && !empty($form_state['values']['pattern_type']) ? $form_state['values']['pattern_type'] : BookingDTO::EVERY_X_DAYS,
  ];
// Frequency.
  $form['schedule']['options']['frequency'] = [
    '#attributes' => [
      'class' => ['js-select2'],
    ],
    '#options' => ['' => t('-- Select --')] + $freq_options,
    '#default_value' => '',
    '#type' => 'select',
    '#title' => t('Frequency') . ' ' . theme_form_required_marker([]),
    '#states' => [
      'disabled' => [
        ':input[name="pattern_type"]' => ['value' => BookingDTO::PICK_DATE_RANGE],
      ],
      'required' => [
        ':input[name="pattern_type"]' => [['value' => BookingDTO::EVERY_X_DAYS], ['value' => BookingDTO::EVERY_X_WEEKS]],
      ],
    ],
  ];
// Pattern days.
  $form['schedule']['options']['pattern_days'] = [
    '#attributes' => [
      'class' => ['js-select2'],
    ],
    '#type' => 'select',
    '#title' => t('Day') . ' ' . theme_form_required_marker([]),
    '#options' => [
      '' => t('-- Select --'),
      BookingDTO::DAY_MONDAY => t('Monday'),
      BookingDTO::DAY_TUESDAY => t('Tuesday'),
      BookingDTO::DAY_WEDNESDAY => t('Wednesday'),
      BookingDTO::DAY_THURSDAY => t('Thursday'),
      BookingDTO::DAY_FRIDAY => t('Friday'),
      BookingDTO::DAY_SATURDAY => t('Saturday'),
      BookingDTO::DAY_SUNDAY => t('Sunday'),
    ],
    '#states' => [
      'enabled' => [
        ':input[name="pattern_type"]' => ['value' => BookingDTO::EVERY_X_WEEKS],
      ],
      'required' => [
        ':input[name="pattern_type"]' => ['value' => BookingDTO::EVERY_X_WEEKS],
      ],
    ],
  ];
4
  • Now returns the first option, "frequency" enable and "pattern_days" disabled. Probably could work saving the value in form_state['storage'] in the AJAX callback. I'm a little bit lost now.
    – Javier Rey
    Commented Aug 17, 2018 at 10:43
  • I didn't get you here... What are you saying is getting the disabled value in $form_state? If that is the case, you won't get the disabled Value in $form_state.
    – Ajay Reddy
    Commented Aug 17, 2018 at 10:46
  • Get the selected radio element and store it in the ajax callback. So then I could set it as "default_value" or smth into the form.
    – Javier Rey
    Commented Aug 17, 2018 at 10:50
  • Not possible. Disabled fields do not get submitted in form submit. The only way is to hide the secondary fields (create 2 more fields and hide that) and with JS you have to alter hidden field values. In this way you get the values,
    – Ajay Reddy
    Commented Aug 17, 2018 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.