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

SVN:trunk[2793]
This commit is contained in:
Romain Quetiez
2013-07-08 08:25:13 +00:00
parent d82326bfd4
commit 3066240ca0
2 changed files with 58 additions and 5 deletions

View File

@@ -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(
<<<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;
$('#$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);
$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(
<<<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;
$('#$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(
<<<EOF
$('#$sId').bind('change validate', function() { ValidateWithPattern('$sId', $sMandatory, '', '$sFormId'); } );
$('#$sId').bind('change validate', function() { ValidateWithPattern('$sId', $sMandatory, '', '$sFormId', null, null); } );
EOF
);
return array('label' => $this->sLabel, 'value' => $sHtml);

View File

@@ -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);