From 3066240ca0866d8bb12d72cf1c455258445298b3 Mon Sep 17 00:00:00 2001 From: Romain Quetiez Date: Mon, 8 Jul 2013 08:25:13 +0000 Subject: [PATCH] Forms: added the possibility to specify forbidden values + message to explain the issue(toolip) SVN:trunk[2793] --- application/forms.class.inc.php | 36 ++++++++++++++++++++++++++++++--- js/property_field.js | 27 +++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/application/forms.class.inc.php b/application/forms.class.inc.php index 180203a00..70cd52920 100644 --- a/application/forms.class.inc.php +++ b/application/forms.class.inc.php @@ -618,27 +618,47 @@ class DesignerLabelField extends DesignerFormField class DesignerTextField extends DesignerFormField { protected $sValidationPattern; + protected $aForbiddenValues; + protected $sExplainForbiddenValues; public function __construct($sCode, $sLabel = '', $defaultValue = '') { parent::__construct($sCode, $sLabel, $defaultValue); $this->sValidationPattern = ''; + $this->aForbiddenValues = null; + $this->sExplainForbiddenValues = null; } public function SetValidationPattern($sValidationPattern) { $this->sValidationPattern = $sValidationPattern; } + + public function SetForbiddenValues($aValues, $sExplain) + { + $this->aForbiddenValues = $aValues; + $this->sExplainForbiddenValues = $sExplain; + } public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog') { $sId = $this->oForm->GetFieldId($this->sCode); $sName = $this->oForm->GetFieldName($this->sCode); $sPattern = addslashes($this->sValidationPattern); + if (is_array($this->aForbiddenValues)) + { + $sForbiddenValues = json_encode($this->aForbiddenValues); + $sExplainForbiddenValues = addslashes($this->sExplainForbiddenValues); + } + else + { + $sForbiddenValues = 'null'; + $sExplainForbiddenValues = 'null'; + } $sMandatory = $this->bMandatory ? 'true' : 'false'; $sReadOnly = $this->IsReadOnly() ? 'readonly' : ''; $oP->add_ready_script( <<oForm->GetFieldId($this->sCode); $sName = $this->oForm->GetFieldName($this->sCode); $sPattern = addslashes($this->sValidationPattern); + if (is_array($this->aForbiddenValues)) + { + $sForbiddenValues = json_encode($this->aForbiddenValues); + $sExplainForbiddenValues = addslashes($this->sExplainForbiddenValues); + } + else + { + $sForbiddenValues = 'null'; + $sExplainForbiddenValues = 'null'; + } $sMandatory = $this->bMandatory ? 'true' : 'false'; $sReadOnly = $this->IsReadOnly() ? 'readonly' : ''; $oP->add_ready_script( <<add_ready_script( << $this->sLabel, 'value' => $sHtml); diff --git a/js/property_field.js b/js/property_field.js index 3c2dfab9c..09c651ee5 100644 --- a/js/property_field.js +++ b/js/property_field.js @@ -213,30 +213,53 @@ $(function() var oFormValidation = {}; -function ValidateWithPattern(sFieldId, bMandatory, sPattern, sFormId) +function ValidateWithPattern(sFieldId, bMandatory, sPattern, sFormId, aForbiddenValues, sExplainForbiddenValues) { var currentVal = $('#'+sFieldId).val(); var bValid = true; + var sMessage = null; if (bMandatory && (currentVal == '')) { bValid = false; } - if ((sPattern != '') && (currentVal != '')) { re = new RegExp(sPattern); bValid = re.test(currentVal); } + if (aForbiddenValues) + { + for(var i = 0; i < aForbiddenValues.length; i++) + { + if (aForbiddenValues[i] == currentVal) + { + bValid = false; + sMessage = sExplainForbiddenValues; + break; + } + } + } + if (oFormValidation[sFormId] == undefined) oFormValidation[sFormId] = []; if (!bValid) { $('#v_'+sFieldId).addClass('ui-state-error'); oFormValidation[sFormId].push(sFieldId); + if (sMessage) + { + $('#'+sFieldId).attr('title', sMessage).tooltip(); + if ($('#'+sFieldId).is(":focus")) + { + $('#'+sFieldId).tooltip('open'); + } + } } else { $('#v_'+sFieldId).removeClass('ui-state-error'); + $('#'+sFieldId).tooltip('close'); + $('#'+sFieldId).removeAttr('title'); // Remove the element from the array iFieldIdPos = oFormValidation[sFormId].indexOf(sFieldId); oFormValidation[sFormId].splice(iFieldIdPos, 1);