Form API - addElement without label?

Form API - addElement without label?

by Dustin Binge -
Number of replies: 4

Hello!

From the docs, it doesn't seem like you can, but.... When using the addElement method, a label and element get generated. For layout purposes, is there anyway to skip the addition of the element's label? For example, I just want to display a multi-select box. When I use the method with a type 'select' I get 2 divs in a parent container - one with a label (with class 'fitemtitle'), and one containing the select (with classes 'felement, fselect').  Any way to get the fitemtitle div dropped out?


I'd rather use the built in methods for adding elements, but I guess if I need to I could always manually create the select object using the html_ writer. Any suggestions/be practices would be appreciated.


Thanks!

Dustin

Average of ratings: -
In reply to Dustin Binge

Re: Form API - addElement without label?

by Darko Miletić -

You can not do that with forms api as is. If you just want to have element without visible title set it to ' ' .

In reply to Dustin Binge

Re: Form API - addElement without label?

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

There are a few things you could try - one would be to give the label an empty string, then use some CSS to remove that label div from the display (from memory, there is a 'nolabel' class, or something like that added, when the label is empty).

Another option, if you have several items and you want to put them together, without individual labels, is to use a group (the group gets a label, but the other items don't need them).

Finally, you could manually code a select + use a 'html' element in the form to output it (this is a bad idea though, as you lose all the benefits of forms - built-in validation, workflow support, etc.).

In reply to Dustin Binge

Re: Form API - addElement without label?

by Dustin Binge -

Thanks guys!


I think grouping the items is the best way for me to go. It's not so much as not wanting the label text, it's really the fact that I have an extra div that takes up unneeded space.

In reply to Dustin Binge

Re: Form API - addElement without label?

by David Mudrák -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Plugins guardians Picture of Testers Picture of Translators

On the other hand, note that form labels are there for good reasons, the accessibility being one of them. I learnt that if you try an write Behat feature test for your form and realize that you have to use XPATH as the only way to describe where the element is on the screen (instead of referring to it in more natural way), there is probably usability and accessibility issue with the form (regardless how compact and modern it looks).