CFC Lookup - When Not Found

digitalwise
digitalwise used Ask the Experts™
on
We have a CFC that we are using to look up a school - I want to limit it so they can't enter something that isn't in the lookup.

Form fields

input type="text" class="form-control" id="schoolname" name="schoolname" value="#getapp.showipeds#" />
    <input type="hidden" class="form-control" name="AI_IPEDS" id="AI_IPEDS" value="#getapp.AI_IPEDS#" />

Open in new window


jquery

$("#schoolname").autocomplete({
            source: function(request, response){
                $.ajax({
                    url: "ipeds.cfc?method=companyNameList",
                    dataType: "json",
                    data: {
                        searchterm: request.term
                    },
                    success: function(data){
                        response(data);
                    }
                })
            },
            minLength: 3,
            select: function(event, ui) {
                $('#AI_IPEDS').val(ui.item.id);
                $('#schoolname').val(ui.item.label);
            }
        });

Open in new window


CFC

<cffunction name="companyNameList" access="remote" output="false" hint="I return a list of companies with IDs" returnformat="JSON">
    <cfargument name="searchterm" required="false" default="" />

      <cfset var returnArray = arrayNew(1) />
    
     
<cfquery name="qryStates" dataSource="mcsf">
    Select schoolname + ' (' + schoolcity + ', ' + schoolstate + ')' as showme, ipedsid from College_IPEDS where schoolname like <cfqueryparam value="%#arguments.searchterm#%" cfsqltype="cf_sql_varchar"> order by schoolname, schoolstate
</cfquery>
 
 <CFIF qryStates.recordcount>
<cfloop query="qryStates">
    <cfset statesStruct = StructNew() />
    <cfset statesStruct["id"] = ipedsid />
    <cfset statesStruct["label"] = showme />
     
    <cfset ArrayAppend(returnArray,statesStruct) />
</cfloop>
<CFELSE>
<cfset statesStruct = StructNew() />
    <cfset statesStruct["id"] = 0 />
    <cfset statesStruct["label"] = showme />
     
    <cfset ArrayAppend(returnArray,statesStruct) />
</CFIF>
 
<cfreturn returnArray />
</cffunction>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
James RodgersWeb Applications Developer
Commented:
you can use the reposnse event in the autocomplete to determine if the returned list is empty or not, if  it is empty process as an error or let the user  know that this is not a valid entry also set a flag to false and do not allow the form to submit so long as the flag is false

http://api.jqueryui.com/autocomplete/#event-response

not tried and just off the top of my head
//global var
// allow_submit=false; // prevent form submit by default

$( "#schoolname" ).autocomplete({
  response: function( event, ui ) {
allow_submit=true; //allow teh form to be submitted until it is false
$('#schoolname_warning').html(''); // reset  warning  message
if(!ui){
// list is empty
allow_submit=false;
// show warning
$('#schoolname_warning').html('<span style="color:red">The entered schooname is not a valid selection</span>');
}
}
});

Open in new window

Author

Commented:
I need to be able to reset the value of the hidden field in case they come back and change it to something not in the list.   A warning will not do that.
Something like this works:
You'll see the response event is being used as Jester suggested.
You can set your values to anything - like a zero instead of an empty string if that's desireable ...

      $("#schoolname").autocomplete({
            source: function(request, response){
                $.ajax({
                    url: "ipeds.cfc?method=companyNameList",
                    dataType: "json",
                    data: {
                        searchterm: request.term
                    },
                    success: function(data){
                        response(data);
                    }
                })
            },
            minLength: 3,
            select: function(event, ui) {
                $('#AI_IPEDS').val(ui.item.id);
                $('#schoolname').val(ui.item.label);
            }
                  
                  /*additional code starts here*/
                  ,
                  response: function(event, ui) {
                  if(ui.content.length === 0) {
                        $('#AI_IPEDS').val('');
                $('#schoolname').val('');
                        }
                  }
                  /*and ends here*/
                  
        });

good luck ...

Author

Commented:
Thanks!  I had to make a tweak in the CFC but that did the trick.
Glad it worked for you !

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial