Setting the "class" attribute on a formslib date_selector element

Setting the "class" attribute on a formslib date_selector element

by Olumuyiwa Taiwo -
Number of replies: 3
Picture of Plugin developers
Hi.

I'm trying to set the class attribute on a date_selector element in a moodleform-derived form class, but this code does not work:

$mform->addElement('date_selector', 'startdate', get_string('startdate', 'local'), null, 'class="sdate"');

Neither does this:

$mform->addElement('date_selector', 'startdate', get_string('startdate', 'local'), null);
$mform->updateElementAttr('startdate', 'class="sdate"');

I'm trying to set the class attribute of the element so that I can use CSS to position it.

Thanks for any pointers.

Muyi

Average of ratings: -
In reply to Olumuyiwa Taiwo

Re: Setting the "class" attribute on a formslib date_selector element

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Formslib gives you quite a lot of useful class and id attributes automatically, but I know it is a major annoyance that there is no id on the outer-most div for each form element to identify what it is.

However, because formslib adds a lot of its own classes and ids, that is probalby why it is ignoring the extra ones you specify - especially if you try to send it as a string 'class="sdate"' - it is going to be very hard for formslib to combine that with its own classes.

I would have to look at the formslib code to say more - so I suggest you do that.
In reply to Tim Hunt

Re: Setting the "class" attribute on a formslib date_selector element

by alberto alberto -

Hi I have looked into formslib.php, and also in the quickforms files, but found nowhere a list of the style classes i can implement on a formslib instance...

If you take a look at this very window (I mean, this forum window), at the right you will see a navigation tree, which uses arrors to hide/show some sections. I would like to do something like that on a formslib instance. I am aware of the setAdvanced options, but the problem is that it hides/shows all items in a form at once, and I would like different sections to be hidden/shown but not all at once: maybe hide one section and show other three (that are 'hideable' as well). So I would like to implement the css that allows this kind of navigation...

Cheers

Alberto

In reply to alberto alberto

Re: Setting the "class" attribute on a formslib date_selector element

by Frank Ralf -
Hi Alberto,

Usually you can add an array of attributes as the last parameter for any form element:

"You can supply a fifth param of attributes here as well."
http://docs.moodle.org/en/Development:lib/formslib.php_Form_Definition#date_selector

hth
Frank

PS.
You might also have a look at Moodle forms - A peek under the hood.