Looking at the documentation for #disabled in the form API documentation, one finds this:

Setting #disabled to TRUE results in user input being ignored, regardless of how the element is themed or whether JavaScript is used to change the control's attributes.

If I'm reading this correctly, this seems to indicate that this is different from the HTML disabled property.

If I want to make an element disabled, to later enable it via javascript, and have its value come through in $form_state['values'], I should not use this?


Yes it's different, as well as rendering the attribute the form API also ensures server-side validation is taken care of.

As with all Drupal form manipulation, the right way to remove the disabled attribute is with the Ajax API, not by manipulating the input directly with JavaScript.

Once you use that method, you'll be able to get values for the input(s) in the form state as normal.

| improve this answer | |
  • Say I have checkboxes that toggle different sets of inputs. The Drupal Way would be to trigger the ajax callback on those elements? – user1359 Oct 31 '16 at 19:36
  • Correct, as the docs say: As a developer you don't use or touch any JavaScript to create an Ajax-enabled form. Drupal does all the work for you. You only need to write php for what you want, and the form API should have you covered for most eventualities – Clive Oct 31 '16 at 19:39
  • I don't want an ajax enabled form. I just want a checkbox to toggle a set of fields. I don't want Drupal doing four form submits, each with a drupal_bootstrap, just to enable four text fields. – user1359 Oct 31 '16 at 19:41
  • If you want to change a sensitive attribute on a form built with the form API, it can only be done via Ajax. It's just how the thing was built – Clive Oct 31 '16 at 19:43
  • So there's no getting around a full bootstrap to enable a field? – user1359 Oct 31 '16 at 19:44

For anyone else in a situation similar to mine, the #states FAPI key can do a lot of what you want, without AJAX callbacks.

Any form element that is disabled in JavaScript should be validated by the back end

| improve this answer | |
  • 1
    You should mention in your answer that any form element that is disabled in JavaScript should be validated by the back end. – mradcliffe Oct 31 '16 at 20:11

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.