0

I am trying to add the boolean attribute "novalidate" to all my forms.

I have tried numerous approaches in my theme_preprocess_form function but no luck. I see in some preprocess functions, $variables['attribute'] is an object and has methods like addClass() and setAttribute(). But in _preprocess_form it is an array. Plus setAttribute doesnt sound like it will do boolean attributes anyway.

I tried to simply override the twig template for a form and do this:

<form{{ attributes }} novalidate>
  {{ children }}
</form>

but even that outputs:

<form novalidate="">
5
  • 2
    novalidate="" is fine for the HTML spec - boolean attributes can have a value as long as it's an empty string or the same (case-insensitive) string as the attribute name. So novalidate="novalidate" would also be fine. If you follow those rules, the attribute's existence is taken to mean true regardless of the value. See developer.mozilla.org/en-US/docs/Web/HTML/…
    – Clive
    Mar 1, 2022 at 20:02
  • You should put that as an answer Clive - it's the correct answer to the question.
    – Jaypan
    Mar 1, 2022 at 20:21
  • I was in two minds @Jaypan, maybe the "proper" answer to this is one which allows Twig/Drupal to output the bare novalidate, even though it doesn't technically matter? Probably over-thinking it, I'll add an answer
    – Clive
    Mar 1, 2022 at 20:36
  • Well, if someone comes along with that other answer, then it too can be an answer to the question. Both will be valid answers.
    – Jaypan
    Mar 1, 2022 at 20:44
  • Your browsers' dev consoles usually render it as <form novalidate="">, but it is in fact still <form novalidate>. You can use the ancient method of viewing the source ("View Page Source" in Firefox and Google Chrome) to see the actual HTML markup.
    – Inan
    Aug 16, 2023 at 8:22

1 Answer 1

4

You may be attempting to solve a non-issue here. novalidate="" is fine for the HTML spec - boolean attributes can have a value as long as it's an empty string or the same (case-insensitive) string as the attribute name. So novalidate="novalidate" would also be fine.

If you follow those rules, the attribute's existence is taken to mean true, regardless of the value.

See https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes#boolean_attributes for more info.

2
  • Yes, I figured this would work functionally although I see there are a few issues on D.org with people asking how to add "boolean attributes" as it is supported in HTML5 and was wondering if there was a way to do this the "right" way.
    – liquidcms
    Mar 1, 2022 at 20:57
  • Yeah it's the sort of thing I'd want too - it's my HTML, give me what I want! But there's too much in the way that could be manipulating the HTML to worry about it IMHO. For example, when you parse HTML into a DOMDocument, it's held in an XML structure. Attributes can't have empty values in XML, so boolean HTML attributes are given an empty value by default, and when the doc is saved as HTML, you get what you've seen in your question. Drupal uses DOMDocument in various parts of the pipeline, so that might even be literally what's happening here. Too much effort to change I'd say
    – Clive
    Mar 1, 2022 at 21:19

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.