Pārlūkot izejas kodu

Forms: added the possibility to specify forbidden values + message to explain the issue(toolip)

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@2793 a333f486-631f-4898-b8df-5754b55c2be0
romainq 12 gadi atpakaļ
vecāks
revīzija
5539f97ac9
2 mainītis faili ar 58 papildinājumiem un 5 dzēšanām
  1. 33 3
      application/forms.class.inc.php
  2. 25 2
      js/property_field.js

+ 33 - 3
application/forms.class.inc.php

@@ -618,27 +618,47 @@ class DesignerLabelField extends DesignerFormField
 class DesignerTextField extends DesignerFormField
 class DesignerTextField extends DesignerFormField
 {
 {
 	protected $sValidationPattern;
 	protected $sValidationPattern;
+	protected $aForbiddenValues;
+	protected $sExplainForbiddenValues;
 	public function __construct($sCode, $sLabel = '', $defaultValue = '')
 	public function __construct($sCode, $sLabel = '', $defaultValue = '')
 	{
 	{
 		parent::__construct($sCode, $sLabel, $defaultValue);
 		parent::__construct($sCode, $sLabel, $defaultValue);
 		$this->sValidationPattern = '';
 		$this->sValidationPattern = '';
+		$this->aForbiddenValues = null;
+		$this->sExplainForbiddenValues = null;
 	}
 	}
 	
 	
 	public function SetValidationPattern($sValidationPattern)
 	public function SetValidationPattern($sValidationPattern)
 	{
 	{
 		$this->sValidationPattern = $sValidationPattern;
 		$this->sValidationPattern = $sValidationPattern;
 	}
 	}
+
+	public function SetForbiddenValues($aValues, $sExplain)
+	{
+		$this->aForbiddenValues = $aValues;
+		$this->sExplainForbiddenValues = $sExplain;
+	}
 	
 	
 	public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
 	public function Render(WebPage $oP, $sFormId, $sRenderMode='dialog')
 	{
 	{
 		$sId = $this->oForm->GetFieldId($this->sCode);
 		$sId = $this->oForm->GetFieldId($this->sCode);
 		$sName = $this->oForm->GetFieldName($this->sCode);
 		$sName = $this->oForm->GetFieldName($this->sCode);
 		$sPattern = addslashes($this->sValidationPattern);
 		$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';
 		$sMandatory = $this->bMandatory ? 'true' :  'false';
 		$sReadOnly = $this->IsReadOnly() ? 'readonly' :  '';
 		$sReadOnly = $this->IsReadOnly() ? 'readonly' :  '';
 		$oP->add_ready_script(
 		$oP->add_ready_script(
 <<<EOF
 <<<EOF
-$('#$sId').bind('change keyup validate', function() { ValidateWithPattern('$sId', $sMandatory, '$sPattern', '$sFormId'); } );
+$('#$sId').bind('change keyup validate', function() { ValidateWithPattern('$sId', $sMandatory, '$sPattern', '$sFormId', $sForbiddenValues, '$sExplainForbiddenValues'); } );
 {
 {
 	var myTimer = null;
 	var myTimer = null;
 	$('#$sId').bind('keyup', function() { clearTimeout(myTimer); myTimer = setTimeout(function() { $('#$sId').trigger('change', {} ); }, 100); });
 	$('#$sId').bind('keyup', function() { clearTimeout(myTimer); myTimer = setTimeout(function() { $('#$sId').trigger('change', {} ); }, 100); });
@@ -671,11 +691,21 @@ class DesignerLongTextField extends DesignerTextField
 		$sId = $this->oForm->GetFieldId($this->sCode);
 		$sId = $this->oForm->GetFieldId($this->sCode);
 		$sName = $this->oForm->GetFieldName($this->sCode);
 		$sName = $this->oForm->GetFieldName($this->sCode);
 		$sPattern = addslashes($this->sValidationPattern);
 		$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';
 		$sMandatory = $this->bMandatory ? 'true' :  'false';
 		$sReadOnly = $this->IsReadOnly() ? 'readonly' :  '';
 		$sReadOnly = $this->IsReadOnly() ? 'readonly' :  '';
 		$oP->add_ready_script(
 		$oP->add_ready_script(
 <<<EOF
 <<<EOF
-$('#$sId').bind('change keyup validate', function() { ValidateWithPattern('$sId', $sMandatory, '$sPattern', '$sFormId'); } );
+$('#$sId').bind('change keyup validate', function() { ValidateWithPattern('$sId', $sMandatory, '$sPattern', '$sFormId', $sForbiddenValues, '$sExplainForbiddenValues'); } );
 {
 {
 	var myTimer = null;
 	var myTimer = null;
 	$('#$sId').bind('keyup', function() { clearTimeout(myTimer); myTimer = setTimeout(function() { $('#$sId').trigger('change', {} ); }, 100); });
 	$('#$sId').bind('keyup', function() { clearTimeout(myTimer); myTimer = setTimeout(function() { $('#$sId').trigger('change', {} ); }, 100); });
@@ -766,7 +796,7 @@ class DesignerComboField extends DesignerFormField
 		}
 		}
 		$oP->add_ready_script(
 		$oP->add_ready_script(
 <<<EOF
 <<<EOF
-$('#$sId').bind('change validate', function() { ValidateWithPattern('$sId', $sMandatory, '', '$sFormId'); } );
+$('#$sId').bind('change validate', function() { ValidateWithPattern('$sId', $sMandatory, '', '$sFormId', null, null); } );
 EOF
 EOF
 );
 );
 		return array('label' => $this->sLabel, 'value' => $sHtml);
 		return array('label' => $this->sLabel, 'value' => $sHtml);

+ 25 - 2
js/property_field.js

@@ -213,30 +213,53 @@ $(function()
 
 
 var oFormValidation = {};
 var oFormValidation = {};
 
 
-function ValidateWithPattern(sFieldId, bMandatory, sPattern, sFormId)
+function ValidateWithPattern(sFieldId, bMandatory, sPattern, sFormId, aForbiddenValues, sExplainForbiddenValues)
 {
 {
 	var currentVal = $('#'+sFieldId).val();
 	var currentVal = $('#'+sFieldId).val();
 	var bValid = true;
 	var bValid = true;
+	var sMessage = null;
 	
 	
 	if (bMandatory && (currentVal == ''))
 	if (bMandatory && (currentVal == ''))
 	{
 	{
 		bValid = false;
 		bValid = false;
 	}
 	}
-	
 	if ((sPattern != '') && (currentVal != ''))
 	if ((sPattern != '') && (currentVal != ''))
 	{
 	{
 		re = new RegExp(sPattern);
 		re = new RegExp(sPattern);
 		bValid = re.test(currentVal);
 		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 (oFormValidation[sFormId] == undefined) oFormValidation[sFormId] = [];
 	if (!bValid)
 	if (!bValid)
 	{
 	{
 		$('#v_'+sFieldId).addClass('ui-state-error');
 		$('#v_'+sFieldId).addClass('ui-state-error');
 		oFormValidation[sFormId].push(sFieldId);
 		oFormValidation[sFormId].push(sFieldId);
+		if (sMessage)
+		{
+			$('#'+sFieldId).attr('title', sMessage).tooltip();
+			if ($('#'+sFieldId).is(":focus"))
+			{
+				$('#'+sFieldId).tooltip('open');
+			}
+		}
 	}
 	}
 	else
 	else
 	{
 	{
 		$('#v_'+sFieldId).removeClass('ui-state-error');
 		$('#v_'+sFieldId).removeClass('ui-state-error');
+		$('#'+sFieldId).tooltip('close');
+		$('#'+sFieldId).removeAttr('title');
 		// Remove the element from the array 
 		// Remove the element from the array 
 		iFieldIdPos = oFormValidation[sFormId].indexOf(sFieldId);
 		iFieldIdPos = oFormValidation[sFormId].indexOf(sFieldId);
 		oFormValidation[sFormId].splice(iFieldIdPos, 1);
 		oFormValidation[sFormId].splice(iFieldIdPos, 1);