Преглед на файлове

Fixed Trac #307: prevent form submission when the value typed into an autocomplete field does not correspond to a valid value.

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@930 a333f486-631f-4898-b8df-5754b55c2be0
dflaven преди 14 години
родител
ревизия
59c3b40999
променени са 3 файла, в които са добавени 30 реда и са изтрити 3 реда
  1. 1 1
      application/cmdbabstract.class.inc.php
  2. 1 1
      css/jquery.autocomplete.css
  3. 28 1
      js/forms-json-utils.js

+ 1 - 1
application/cmdbabstract.class.inc.php

@@ -1128,7 +1128,7 @@ EOF
 							$sHTMLValue .= "<input type=\"hidden\" id=\"$iId\" name=\"attr_{$sFieldPrefix}{$sAttCode}{$sNameSuffix}\" value=\"$value\" />\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

+ 1 - 1
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 {

+ 28 - 1
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(''); //<img src="../images/validation_ok.png" />');
 	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');
+	}
+}