Can't create checked checkboxes

Can't create checked checkboxes

Luca Celenza tomonidan -
Number of replies: 5
Hi everybody. I'm stuck with a problem I'm sure it has an obvious solution (which I can't seem to find, however): I can't create a checked checkbox in a form. What I want to accomplish is a group of checked checkboxes representing the days of the week. I'm using the code below with no success:


$wdays = array();

$wdays[] =& $mform->createElement('checkbox', 'Mon', '', get_string('monday','calendar'), 'checked="checked"');
$wdays[] =& $mform->createElement('checkbox', 'Tue', '', get_string('tuesday','calendar'), 'checked="checked"');
$wdays[] =& $mform->createElement('checkbox', 'Wed', '', get_string('wednesday','calendar'), 'checked="checked"');
$wdays[] =& $mform->createElement('checkbox', 'Thu', '', get_string('thursday','calendar'), 'checked="checked"');
$wdays[] =& $mform->createElement('checkbox', 'Fri', '', get_string('friday','calendar'), 'checked="checked"');
$wdays[] =& $mform->createElement('checkbox', 'Sat', '', get_string('saturday','calendar'), 'checked="checked"');
$wdays[] =& $mform->createElement('checkbox', 'Sun', '', get_string('sunday','calendar'), 'checked="checked"');

$mform->addGroup($wdays, 'wdays', 'Days of the week', array(' '), true);


This code displays fine the checkboxes and all, but no one of them is checked. Any suggestion? Thanks in advance.
O'rtacha reytinglar: -
In reply to Luca Celenza

Re: Can't create checked checkboxes

Davo Smith tomonidan -
Core developers rasmi Particularly helpful Moodlers rasmi Peer reviewers rasmi Plugin developers rasmi
I'm not an expert at Moodle Forms, but I'm guessing you might need to add a "$mform->setDefault('Mon', true)" line just after each one.
In reply to Davo Smith

Re: Can't create checked checkboxes

Luca Celenza tomonidan -
Worked like a charm. Really thank you!
For the ones still wondering here's the code:


$wdays = array();

//creating days of the week
$wdays[] =& $mform->createElement('checkbox', 'Mon', '', get_string('monday','calendar'));
$wdays[] =& $mform->createElement('checkbox', 'Tue', '', get_string('tuesday','calendar'));
$wdays[] =& $mform->createElement('checkbox', 'Wed', '', get_string('wednesday','calendar'));
$wdays[] =& $mform->createElement('checkbox', 'Thu', '', get_string('thursday','calendar'));
$wdays[] =& $mform->createElement('checkbox', 'Fri', '', get_string('friday','calendar'));
$wdays[] =& $mform->createElement('checkbox', 'Sat', '', get_string('saturday','calendar'));
$wdays[] =& $mform->createElement('checkbox', 'Sun', '', get_string('sunday','calendar'));

//display them into one row
$mform->addGroup($wdays, 'wdays', 'Days of the week', array(' '), true);

//check every day of the week
$mform->setDefault('wdays[Mon]', true);
$mform->setDefault('wdays[Tue]', true);
$mform->setDefault('wdays[Wed]', true);
$mform->setDefault('wdays[Thu]', true);
$mform->setDefault('wdays[Fri]', true);
$mform->setDefault('wdays[Sat]', true);
$mform->setDefault('wdays[Sun]', true);
In reply to Luca Celenza

Re: Can't create checked checkboxes

Daniele Cordella tomonidan -
Core developers rasmi Plugin developers rasmi
or you can display the form using

$defaults = new object();
$defaults->wdays = 'Mon';
$mform->set_data($defaults);
$mform->display();

if I well remember ammiccante
In reply to Luca Celenza

Re: Can't create checked checkboxes

Frank Ralf tomonidan -
Your codes marks all of your checkboxes as "checked" which is impossible to render for the browser. Only add the attribute for your default value and omit the attribute entirely for the other checkboxes.

hth
Frank
In reply to Frank Ralf

Re: Can't create checked checkboxes

David Druckenmiller tomonidan -
I realize this thread has been silent since April, but I've run into the same issue. I am able to work around it by using Davo Smith's suggestion of using:
$mform->setDefault('Mon',true);

However, I'm still wondering why passing 'checked="checked"' as the attribute parameter doesn't work for check-boxes. If I pass 'disabled="disabled"' as the attribute parameter the input is correctly disabled, so the attribute IS being used as it should; just the 'checked' attribute doesn't work.

In response to Frank's comment about browser not being able to render this, that is not accurate because we're talking about check-boxes. If we were talking about radio buttons, then you're absolutely right--but check-boxes can all be checked.

If I use the setDefault() function to check the box and then view the source in the browser, lo and behold the attribute checked="checked" is now present.

It's possible that I'm missing something, but it seems like a minor bug to me.

</rant>

Dave