From 0b95b3799e9d2512b14ad53eeada2ebc4fce3608 Mon Sep 17 00:00:00 2001 From: Denis Flaven Date: Tue, 26 Oct 2010 08:12:31 +0000 Subject: [PATCH] Fixed Trac #307: prevent form submission when the value typed into an autocomplete field does not correspond to a valid value. SVN:trunk[930] --- application/cmdbabstract.class.inc.php | 2 +- css/jquery.autocomplete.css | 2 +- js/forms-json-utils.js | 29 +++++++++++++++++++++++++- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/application/cmdbabstract.class.inc.php b/application/cmdbabstract.class.inc.php index dba2b58b6..c51106bf7 100644 --- a/application/cmdbabstract.class.inc.php +++ b/application/cmdbabstract.class.inc.php @@ -1128,7 +1128,7 @@ EOF $sHTMLValue .= "\n"; $oPage->add_ready_script("\$('#label_$iId').autocomplete('./ajax.render.php', { scroll:true, minChars:3, onItemSelect:selectItem, onFindValue:findValue, formatItem:formatItem, autoFill:true, keyHolder:'#$iId', extraParams:{operation:'autocomplete', sclass:'$sClass',attCode:'".$sAttCode."'}});"); $oPage->add_ready_script("\$('#label_$iId').blur(function() { $(this).search(); } );"); - $oPage->add_ready_script("\$('#label_$iId').result( function(event, data, formatted) { if (data) { $('#{$iId}').val(data[1]); $('#{$iId}').trigger('change'); } else { $('#{$iId}').val(''); $('#{$iId}').trigger('change');} } );"); + $oPage->add_ready_script("\$('#label_$iId').result( function(event, data, formatted) { OnAutoComplete('$iId', event, data, formatted); } );"); $aEventsList[] ='change'; } else diff --git a/css/jquery.autocomplete.css b/css/jquery.autocomplete.css index 91b622833..4e6b634f1 100644 --- a/css/jquery.autocomplete.css +++ b/css/jquery.autocomplete.css @@ -35,7 +35,7 @@ } .ac_loading { - background: white url('indicator.gif') right center no-repeat; + background: white url('../images/indicator.gif') right center no-repeat; } .ac_odd { diff --git a/js/forms-json-utils.js b/js/forms-json-utils.js index 6a82e0a84..9fbf187af 100644 --- a/js/forms-json-utils.js +++ b/js/forms-json-utils.js @@ -154,7 +154,11 @@ function ValidateField(sFieldId, sPattern, bMandatory, sFormId, nullValue) { var bValid = true; var currentVal = $('#'+sFieldId).val(); - if (bMandatory && (currentVal == nullValue)) + if (currentVal == '$$NULL$$') // Convention to indicate a non-valid value since it may have to be passed as text + { + bValid = false; + } + else if (bMandatory && (currentVal == nullValue)) { bValid = false; } @@ -248,3 +252,26 @@ function ValidatePasswordField(id, sFormId) $('#v_'+id).html(''); //'); return true; } + +// Called when filling an autocomplete field +function OnAutoComplete(id, event, data, formatted) +{ + if (data) + { + // A valid match was found: data[0] => label, data[1] => value + $('#'+id).val(data[1]); + $('#'+id).trigger('change'); + } + else + { + if ($('#label_'+id).val() == '') + { + $('#'+id).val(''); // Empty value + } + else + { + $('#'+id).val('$$NULL$$'); // Convention: not a valid value + } + $('#'+id).trigger('change'); + } +} \ No newline at end of file