mirror of
https://github.com/EGroupware/egroupware.git
synced 2024-12-23 15:18:58 +01:00
implement clientside validation for textbox, stop complaining about $ in regular expressions of validator and fix attribute name from "preg" to "validator" on server-side
This commit is contained in:
parent
76fc9f97dc
commit
a45525949b
@ -97,7 +97,7 @@ class etemplate_widget_textbox extends etemplate_widget
|
||||
* - needed: value must NOT be empty
|
||||
* - min, max: int and float widget only
|
||||
* - maxlength: maximum length of string (longer strings get truncated to allowed size)
|
||||
* - preg: perl regular expression incl. delimiters (set by default for int, float and colorpicker)
|
||||
* - validator: perl regular expression incl. delimiters (set by default for int, float and colorpicker)
|
||||
* - int and float get casted to their type
|
||||
*
|
||||
* @param string $cname current namespace
|
||||
@ -112,18 +112,19 @@ class etemplate_widget_textbox extends etemplate_widget
|
||||
|
||||
if (!$this->is_readonly($cname, $form_name))
|
||||
{
|
||||
if (!isset($this->attrs['preg']))
|
||||
if (!isset($this->attrs['validator']))
|
||||
{
|
||||
switch($this->type)
|
||||
{
|
||||
case 'int':
|
||||
case 'integer':
|
||||
$this->attrs['preg'] = '/^-?[0-9]*$/';
|
||||
$this->attrs['validator'] = '/^-?[0-9]*$/';
|
||||
break;
|
||||
case 'float':
|
||||
$this->attrs['preg'] = '/^-?[0-9]*[,.]?[0-9]*$/';
|
||||
$this->attrs['validator'] = '/^-?[0-9]*[,.]?[0-9]*$/';
|
||||
break;
|
||||
case 'colorpicker':
|
||||
$this->attrs['preg'] = '/^(#[0-9a-f]{6}|)$/i';
|
||||
$this->attrs['validator'] = '/^(#[0-9a-f]{6}|)$/i';
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -150,7 +151,7 @@ class etemplate_widget_textbox extends etemplate_widget
|
||||
{
|
||||
$value = mb_substr($value,0,(int) $this->attrs['maxlength']);
|
||||
}
|
||||
if ($this->attrs['preg'] && !preg_match($this->attrs['preg'],$value))
|
||||
if ($this->attrs['validator'] && !preg_match($this->attrs['validator'],$value))
|
||||
{
|
||||
switch($this->type)
|
||||
{
|
||||
@ -161,7 +162,7 @@ class etemplate_widget_textbox extends etemplate_widget
|
||||
self::set_validation_error($form_name,lang("'%1' is not a valid floatingpoint number !!!",$value),'');
|
||||
break;
|
||||
default:
|
||||
self::set_validation_error($form_name,lang("'%1' has an invalid format !!!",$value)/*." !preg_match('$this->attrs[preg]', '$value')"*/,'');
|
||||
self::set_validation_error($form_name,lang("'%1' has an invalid format !!!",$value)/*." !preg_match('$this->attrs[validator]', '$value')"*/,'');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -70,6 +70,12 @@
|
||||
str += '$';
|
||||
break;
|
||||
}
|
||||
// check for regular expression "/ $/"
|
||||
if (_p.expr.charAt(_p.pos) == _p.expr.charAt(0))
|
||||
{
|
||||
str += '$';
|
||||
break;
|
||||
}
|
||||
if (str)
|
||||
{
|
||||
_tree.push(str); str = "";
|
||||
|
@ -62,10 +62,16 @@ var et2_textbox = et2_inputWidget.extend(
|
||||
"type": "integer",
|
||||
"default": -1,
|
||||
"description": "Multiline field width - better to use CSS"
|
||||
},
|
||||
"validator": {
|
||||
"name": "Validator",
|
||||
"type": "string",
|
||||
"default": et2_no_init,
|
||||
"description": "Perl regular expression eg. '/^[0-9][a-f]{4}$/i'"
|
||||
}
|
||||
},
|
||||
|
||||
legacyOptions: ["size", "maxlength"],
|
||||
legacyOptions: ["size", "maxlength", "validator"],
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
@ -126,6 +132,37 @@ var et2_textbox = et2_inputWidget.extend(
|
||||
return this._super.apply(this, arguments);
|
||||
},
|
||||
|
||||
/**
|
||||
* Clientside validation using regular expression in "validator" attribute
|
||||
*
|
||||
* @param {array} _messages
|
||||
*/
|
||||
isValid: function(_messages)
|
||||
{
|
||||
var ok = true;
|
||||
// Check input is valid
|
||||
if(this.options && this.options.validator && !this.options.readonly)
|
||||
{
|
||||
if (typeof this.options.validator == 'string')
|
||||
{
|
||||
var parts = this.options.validator.split('/');
|
||||
var flags = parts.pop();
|
||||
if (parts.length < 2 || parts[0] !== '')
|
||||
{
|
||||
_messages.push(this.egw().lang("'%1' has an invalid format !!!", this.options.validator));
|
||||
return false; // show invalid expression
|
||||
}
|
||||
parts.shift();
|
||||
this.options.validator = new RegExp(parts.join('/'), flags);
|
||||
}
|
||||
var value = this.getValue();
|
||||
if (!(ok = this.options.validator.test(value)))
|
||||
{
|
||||
_messages.push(this.egw().lang("'%1' has an invalid format !!!", value));
|
||||
}
|
||||
}
|
||||
return this._super.apply(this, arguments) && ok;
|
||||
},
|
||||
|
||||
/**
|
||||
* Set input widget size
|
||||
|
Loading…
Reference in New Issue
Block a user