You are here: Home / Documentation / How To's / Email address autocompletion

Email address autocompletion

UW Oshkosh specific autocomplete for PloneFormGen replyto (email address) field

In our specific use case, we needed to have the standard 'replyto' field in a PloneFormGen form autocomplete based on the last name of a person being typed.

https://www.uwosh.edu/intranet/Members/kimadmin/kims-test-form/view

You'll have to log in.

The email address ("replyto") field defaults to your email address but if you select it all, delete, and start typing, it'll autocomplete with campus email addresses.

Here is the source for email_address_autocomplete.js:

$(function() {
    var cache = {};
    $( "#replyto" ).autocomplete({
      minLength: 2,
      source: function( request, response ) {
        var term = request.term;
  
        $.getJSON( "getMatchingEmailWS?match=" + term + "&callback=?", function( data ) {
            response( $.map( data, function( item ) {
              return {
                label: item.f + " " + item.m + " " + item.l + " <" + item.e + ">",
                value: item.e
              }
            }));
        });
      }
    });
  });
 

which I registered in the Intranet's portal_javascripts so it's loaded only for authenticated users; it attaches itself to any field named 'replyto'

This is the source for getMatchingEmailWS.py:

request = container.REQUEST
response =  request.response

# Run Query
query = str(request.form.get('match','')).replace(' ','')
results = []

# Must be more than 3 characters long
if len(query) > 3:
    members = context.portal_membership.searchForMembers(login=query, limit=2)
    for member in members:
        results.append({ 
            "e" : str(member.getProperty("email"," ")),
            "f" : str(member.getProperty("firstname"," ")),
            "m" : str(member.getProperty("middlename"," ")),
            "l" : str(member.getProperty("lastname"," ")),
        })

return request.form.get('callback', '?') + '(' + str(results) + ')'
 

which is a Script (Python) I added to the Intranet's portal_skins/custom folder; it calls the web service on ws.it.uwosh.edu that returns at most 100 emails that match on last name the string provided (minimum length: two characters) This means that on the Intranet any PFG form that includes the default email address field will have autocomplete. :)

 

Navigation