diff --git a/application/cmdbabstract.class.inc.php b/application/cmdbabstract.class.inc.php index 316753c6b..5f61bb0e2 100644 --- a/application/cmdbabstract.class.inc.php +++ b/application/cmdbabstract.class.inc.php @@ -1938,7 +1938,16 @@ EOF $sHidden = ""; // To know how many entries the case log already contains $sHTMLValue = "
$sHeader$sPreviousLog
{$sValidationSpan}{$sReloadSpan}$sHidden"; - $oPage->add_ready_script("$('#$iId').bind('keyup change validate', function(evt, sFormId) { return ValidateCaseLogField('$iId', $bMandatory, sFormId) } );"); // Custom validation function + + // Note: This should be refactored for all types of attribute (see at the end of this function) but as we are doing this for a maintenance release, we are scheduling it for the next main release in to order to avoid regressions as much as possible. + $sNullValue = $oAttDef->GetNullValue(); + if (!is_numeric($sNullValue)) + { + $sNullValue = "'$sNullValue'"; // Add quotes to turn this into a JS string if it's not a number + } + $sOriginalValue = ($iFlags & OPT_ATT_MUSTCHANGE) ? json_encode($value->GetModifiedEntry('html')) : 'undefined'; + + $oPage->add_ready_script("$('#$iId').bind('keyup change validate', function(evt, sFormId) { return ValidateCaseLogField('$iId', $bMandatory, sFormId, $sNullValue, $sOriginalValue) } );"); // Custom validation function // Replace the text area with CKEditor // To change the default settings of the editor, diff --git a/js/forms-json-utils.js b/js/forms-json-utils.js index 9eb54a9e7..f00e37385 100644 --- a/js/forms-json-utils.js +++ b/js/forms-json-utils.js @@ -297,6 +297,7 @@ function ValidateCKEditField(sFieldId, sPattern, bMandatory, sFormId, nullValue, } else { + // Note: value change check is not working well yet as the HTML to Text conversion is not exactly the same when done from the PHP value or the CKEditor value. sExplain = Dict.S('UI:ValueMustBeChanged'); } } @@ -308,6 +309,7 @@ function ValidateCKEditField(sFieldId, sPattern, bMandatory, sFormId, nullValue, ReportFieldValidationStatus(sFieldId, sFormId, bValid, sExplain); + // We need to check periodically as CKEditor doesn't trigger our events. More details in UIHTMLEditorWidget::Display() @ line 92 setTimeout(function(){ValidateCKEditField(sFieldId, sPattern, bMandatory, sFormId, nullValue, originalValue);}, 500); } @@ -374,32 +376,39 @@ function ValidatePasswordField(id, sFormId) //Special validation function for case log fields, taking into account the history // to determine if the field is empty or not -function ValidateCaseLogField(sFieldId, bMandatory, sFormId) +function ValidateCaseLogField(sFieldId, bMandatory, sFormId, nullValue, originalValue) { - bValid = true; + var bValid = true; + var sExplain = ''; + var sTextContent; if ($('#'+sFieldId).attr('disabled')) { bValid = true; // disabled fields are not checked } - else if (!bMandatory) - { - bValid = true; - } else { - if (bMandatory) + // Get the contents (with tags) + // Note: For CaseLog we can't retrieve the formatted contents from CKEditor (unlike in ValidateCKEditorField() method) because of the place holder. + sTextContent = $('#' + sFieldId).val(); + var count = $('#'+sFieldId+'_count').val(); + + if (bMandatory && (count == 0) && (sTextContent == nullValue)) { - var count = $('#'+sFieldId+'_count').val(); - if ( (count == 0) && ($('#'+sFieldId).val() == '') ) - { - // No previous entry and no content typed - bValid = false; - } + // No previous entry and no content typed + bValid = false; + sExplain = Dict.S('UI:ValueMustBeSet'); + } + else if ((originalValue != undefined) && (sTextContent == originalValue)) + { + bValid = false; + sExplain = Dict.S('UI:ValueMustBeChanged'); } } - ReportFieldValidationStatus(sFieldId, sFormId, bValid, ''); - return bValid; + ReportFieldValidationStatus(sFieldId, sFormId, bValid, '' /* sExplain */); + + // We need to check periodically as CKEditor doesn't trigger our events. More details in UIHTMLEditorWidget::Display() @ line 92 + setTimeout(function(){ValidateCaseLogField(sFieldId, bMandatory, sFormId, nullValue, originalValue);}, 500); } // Validate the inputs depending on the current setting