Note that there are some explanatory texts on larger screens.

plurals
  1. POadding custom function to bootstrap typeahead
    primarykey
    data
    text
    <p>I'm using typeahead to pull names and then updating a hidden field with the id respective to the name. In my form the name can be left blank, filling it is not a requirement.</p> <p>Is there a way to add custom error checking to the typeahead function? for instance, I start filling out the form and type/select a user, then I change my mind and want to leave it blank. by this point, the user id is already added to the hidden field, so even if i leave the name field blank, the form will be passed with the user id in it. </p> <p>I guess I can always do error checking on submit, but am curious if there is a way to add to the native api.</p> <p>my code is:</p> <pre><code>$('#name_field').typeahead({ minLength: 3, source: function (query, process){ $.get('/url/getsource', {query: query}, function (data) { names = []; map = {}; $.each(data, function (i, name) { map[name.uName] = name; names.push(name.uName); }); process(names); }); }, matcher: function (item) { if (item.toLowerCase().indexOf(this.query.trim().toLowerCase()) != -1) { return true; } }, sorter: function (items) { return items.sort(); }, highlighter: function (item) { var regex = new RegExp( '(' + this.query + ')', 'gi' ); return item.replace( regex, "&lt;strong&gt;$1&lt;/strong&gt;" ); }, updater: function (item) { selectedID = map[item].id; $('#id_field').val(selectedID); return item; } }); </code></pre> <p>I was hoping I can add something like</p> <pre><code>$('#name_field').typeahead({ myCustom: function (){ if($('#name_field').val() == ''){ $('#id_field').val(''); }, rest, of the, function }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload