CFC Lookup - When Not Found

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

digitalwiseAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

James RodgersWeb Applications DeveloperCommented:
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

digitalwiseAuthor 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.
dgrafxCommented:
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 ...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
digitalwiseAuthor Commented:
Thanks!  I had to make a tweak in the CFC but that did the trick.
dgrafxCommented:
Glad it worked for you !
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ColdFusion Language

From novice to tech pro — start learning today.