Disable default selection on 'autocomplete' mform element? (single select)

Disable default selection on 'autocomplete' mform element? (single select)

by Peter Stacey -
Number of replies: 5

I'm just posting this as a new thread as the original one has been locked.  I have found   thought I'd found a fix for the problem posted by Jens Eeckhout which I had also had problems with. Original thread here: https://moodle.org/mod/forum/discuss.php?d=345085#p1391592


Setting up a single select autocomplete field in a form it always displayed the first element of the array on loading the page. I discovered from something mentioned in this tracker   that adding 'multiple' => false in the options array  appeared to fix this problem (even though false is the default value for multiple).  However on further testing I realised that it does work, but as a 'multiple' which isn't what I need and despite 'multiple' being set to false.


So it appears that the problem is really with single select autocomplete. Any suggestions gratefully recieved.



Average of ratings: -
In reply to Peter Stacey

Re: Disable default selection on 'autocomplete' mform element? (single select)

by Mitxel Moriana -

I have a strong feeling that this issue is related to the fact that the browser marks as selected the first element of any "single-value" selector, then the autocomplete module just "inherits" this first option marked as selected. A fix/hack is theoretically possible by modifying the autocomplete module (form-autocomplete) initialization like so (not tested):

Check original select is not multiple (it is single), if so then check no option was really marked (yet), if so then prepend dummy empty option (<option></option>) to the underlying selector and mark it as selected (...prop('selected', true)), then prevent the autocomplete to add this item (as the selected one) if it is a dummy/empty item (at updateSelectionList method).

In reply to Peter Stacey

إعادة: Disable default selection on 'autocomplete' mform element? (single select)

by NAMA Coconut -

hello peter, 


the form completion could be disabled from the html file , this is the best practice for example if you want to disable the form completion for login form , 


- depending on the theme you use find the file"liginform.mustache"

-then inside tag , add this parameter " autocomplete="off" 


it's like this for any element you want to disable autocomplete for.

In reply to Peter Stacey

Re: Disable default selection on 'autocomplete' mform element? (single select)

by Elias Valderrama -
Hi Peter,
I followed Mitxel's suggestion and I put my code like this:

// Nombre del docente
$rs = get_enrolled_users($this->context, 'mod/quiz:addinstance');
$docentes[] = '';
foreach ($rs as $docenteid => $rd) {
$docentes[$docenteid] = $rd->firstname . " " . $rd->lastname;
}
$docentes_opciones = array('placeholder' => 'Seleccione al docente');
$mform->addElement('autocomplete', 'teacherid', get_string('teacher_id', 'bgactaasignatura'), $docentes, $docentes_opciones);
Attachment select.png
Average of ratings: Useful (1)
In reply to Peter Stacey

Re: Disable default selection on 'autocomplete' mform element? (single select)

by Jakub Kleban -
$autocomplete = $mform->addElement('autocomplete', 'id', 'label', array('a','b'), $options);
$autocomplete->setValue('');
In reply to Jakub Kleban

Re: Disable default selection on 'autocomplete' mform element? (single select)

by Евгений Мамаев -
Picture of Plugin developers
For me the following is a solution:
Create an array of values for autocomplete and set the first element with zero index.
$values[0] = '';
$values[1] = 'value1';
$values[2] = 'value2';
.........