[how_to] Forms - set date_selector with 'optional' => true to enabled by default?

[how_to] Forms - set date_selector with 'optional' => true to enabled by default?

by Urs Hunkler -
Number of replies: 2
Picture of Core developers

In a date_selector with the option  'optional' set to true to show the »Enabled« checkbox the checkbox should be activate by default. Moodle sets the checkbox to off and I don't find a way to change the default behavior.

I tried several variants of the »disabledIf« statement but without success - I guess because the »Enabled« checkbox is generated internally.

$mform->disabledIf('mycontrol', 'somecheckbox', 'checked');

Is there a possibility to activate »Enabled« by default?

Thanks a lot for any help.

Average of ratings: Useful (1)
In reply to Urs Hunkler

Re: [how_to] Forms - set date_selector with 'optional' => true to enabled by default?

by Darko Miletić -

If you need it enabled you need to set default value.

class myform extends moodleform {
protected function definition() {
$mform = $this->_form;
$opts = array(
'startyear' => 1970,
'stopyear' => 2020,
'timezone' => 99,
'optional' => true,
);

$mform->addElement('date_selector', 'assesstimefinish', 'aaaaaa', $opts);
$mform->setDefault('assesstimefinish', time() + 3600 * 24);

$this->add_action_buttons();
}
}


Average of ratings: Useful (2)
In reply to Darko Miletić

Re: [how_to] Forms - set date_selector with 'optional' => true to enabled by default?

by Urs Hunkler -
Picture of Core developers

Thanks a lot Darko, so easy but not obvious at all.