Score:0

How to add a boolen attribute to a form?

us flag

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="">
cn flag
`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
Jaypan avatar
de flag
You should put that as an answer Clive - it's the correct answer to the question.
cn flag
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
Jaypan avatar
de flag
Well, if someone comes along with that other answer, then it too can be an answer to the question. Both will be valid answers.
Inan avatar
dk flag
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.
Score:3
cn flag

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.

liquidcms avatar
us flag
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.
cn flag
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
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.