Kaynağa Gözat

- Fixed Trac #224: properly validate non-mandatory integer fields.

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@768 a333f486-631f-4898-b8df-5754b55c2be0
dflaven 14 yıl önce
ebeveyn
işleme
1665410b81

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

@@ -1078,7 +1078,12 @@ abstract class cmdbAbstractObject extends CMDBObject
 			$sPattern = addslashes($oAttDef->GetValidationPattern()); //'^([0-9]+)$';
 			if (!empty($aEventsList))
 			{
-				$oPage->add_ready_script("$('#$iId').bind('".implode(' ', $aEventsList)."', function(evt, sFormId) { return ValidateField('$iId', '$sPattern', $bMandatory, sFormId) } );"); // Bind to a custom event: validate
+				$sNullValue = $oAttDef->GetNullValue();
+				if (!is_numeric($sNullValue))
+				{
+					$sNullValue = "'$sNullValue'"; // Add quotes to turn this into a JS string if it's not a number
+				}
+				$oPage->add_ready_script("$('#$iId').bind('".implode(' ', $aEventsList)."', function(evt, sFormId) { return ValidateField('$iId', '$sPattern', $bMandatory, sFormId, $sNullValue) } );"); // Bind to a custom event: validate
 			}
 			$aDependencies = MetaModel::GetDependentAttributes($sClass, $sAttCode); // List of attributes that depend on the current one
 			if (count($aDependencies) > 0)

+ 3 - 3
js/forms-json-utils.js

@@ -150,15 +150,15 @@ function CheckFields(sFormId, bDisplayAlert)
 	return (oFormErrors['err_'+sFormId] == 0); // If no error, submit the form
 }
 
-function ValidateField(sFieldId, sPattern, bMandatory, sFormId)
+function ValidateField(sFieldId, sPattern, bMandatory, sFormId, nullValue)
 {
 	var bValid = true;
 	var currentVal = $('#'+sFieldId).val();
-	if (bMandatory && ((currentVal == '') || (currentVal == 0) || (currentVal == '[]')))
+	if (bMandatory && (currentVal == nullValue))
 	{
 		bValid = false;
 	}
-	else if ((currentVal == '') || (currentVal == 0) || (currentVal == '[]'))
+	else if (currentVal == nullValue)
 	{
 		// An empty field is Ok...
 		bValid = true;