0

I would like to find a way to remove the disabled attribute from webform fields inside a custom Handler.

The disabled attribute is currently being applied to webform elements during user input via conditions/states with the UI and I would like to remove this attribute after clicking submit so that these webform fields will submit their values.

I've been looking through the available functions from the WebformhandlerBase: https://git.uwaterloo.ca/drupal-org/webform/blob/8.x-5.9/src/Plugin/WebformHandlerBase.php, but can't find where this attribute exists, and don't see an approach to alter/remove it.

public function validateForm() {} seems like a good moment in time to alter, but I can't find this attribute.

Noting I have success doing this with jQuery but would prefer doing this in my custom Handler

How do you remove a form field attribute (disabled) with a custom Handler after a user clicks submit?

2
  • And you are definitely not looking for the Clear value(s) when hidden checkbox every element has under its "Conditions" tab and uncheck that, or are you?
    – leymannx
    Commented Apr 12, 2020 at 22:23
  • Hey @leymannx, I don’t think so. I need the field values, but as the field is both required, and disabled as a user fills in the form, I need to remove the disabled attribute so form submit completes Commented Apr 12, 2020 at 22:26

2 Answers 2

0

Once you submit a form, you are not able to retrieve disabled inputs (https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#constructing-the-form-data-set). That said:

  • You cannot modify input's attributes from server-side since the submission data is already posted.
  • Developing your solution with jQuery on .submit() function is a good idea (far away from bad practices).
  • You can also make another work around: instead of disabling you could use readonly="readonly" attribute (https://www.w3.org/TR/REC-html40-971218/interact/forms.html#h-17.12.2)
6
  • Is that true for the validateForm() function? I see that with the field both required and disabled, the form does not submit and is set with an error message. Commented Apr 16, 2020 at 20:24
  • validateForm() makes use of the submitted values. Because you have disabled a field and is not present in the submitted values, that means will not be present in the validateForm() either. If your form is showing an error, check webform_hooks validations along with the webform UI in case you have implementation other validations.
    – user98716
    Commented Apr 18, 2020 at 11:38
  • @Prestosaurus did you find an answer on how to identify and remove disabled fields in the handler, if so could you please share it. thanks
    – amit
    Commented Jan 24, 2023 at 0:18
  • Hi @amit, I did not and had to take a different approach Commented Jan 27, 2023 at 4:00
  • @Prestosaurus we have found a workaround, its a bit tedious but works as a proof of concept. I posted it as answer, hopefully the moderator would reinstate my deleted post.
    – amit
    Commented Jan 31, 2023 at 7:35
0

Here is a workaround that seems to work, for example

  1. Create a number element with key as num_a 2.Create a hidden checkbox element with its key as num_a_ignore
  2. Add a condition to num_a element in #1 to turn on the checkbox when value or num_a is say greater than 10
  3. Write a custom handler which probes for *_ignore keys and matches them with any element keys. When a match is found and the value of *_ignore key is true that element is disabled and processed accordingly.

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.