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:
Ralf Becker
2014-06-05 15:47:23 +00:00
parent 76fc9f97dc
commit a45525949b
3 changed files with 52 additions and 8 deletions

View File

@ -97,7 +97,7 @@ class etemplate_widget_textbox extends etemplate_widget
* - needed: value must NOT be empty * - needed: value must NOT be empty
* - min, max: int and float widget only * - min, max: int and float widget only
* - maxlength: maximum length of string (longer strings get truncated to allowed size) * - 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 * - int and float get casted to their type
* *
* @param string $cname current namespace * @param string $cname current namespace
@ -112,18 +112,19 @@ class etemplate_widget_textbox extends etemplate_widget
if (!$this->is_readonly($cname, $form_name)) if (!$this->is_readonly($cname, $form_name))
{ {
if (!isset($this->attrs['preg'])) if (!isset($this->attrs['validator']))
{ {
switch($this->type) switch($this->type)
{ {
case 'int':
case 'integer': case 'integer':
$this->attrs['preg'] = '/^-?[0-9]*$/'; $this->attrs['validator'] = '/^-?[0-9]*$/';
break; break;
case 'float': case 'float':
$this->attrs['preg'] = '/^-?[0-9]*[,.]?[0-9]*$/'; $this->attrs['validator'] = '/^-?[0-9]*[,.]?[0-9]*$/';
break; break;
case 'colorpicker': case 'colorpicker':
$this->attrs['preg'] = '/^(#[0-9a-f]{6}|)$/i'; $this->attrs['validator'] = '/^(#[0-9a-f]{6}|)$/i';
break; break;
} }
} }
@ -150,7 +151,7 @@ class etemplate_widget_textbox extends etemplate_widget
{ {
$value = mb_substr($value,0,(int) $this->attrs['maxlength']); $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) 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),''); self::set_validation_error($form_name,lang("'%1' is not a valid floatingpoint number !!!",$value),'');
break; break;
default: 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; break;
} }
} }

View File

@ -70,6 +70,12 @@
str += '$'; str += '$';
break; break;
} }
// check for regular expression "/ $/"
if (_p.expr.charAt(_p.pos) == _p.expr.charAt(0))
{
str += '$';
break;
}
if (str) if (str)
{ {
_tree.push(str); str = ""; _tree.push(str); str = "";

View File

@ -62,10 +62,16 @@ var et2_textbox = et2_inputWidget.extend(
"type": "integer", "type": "integer",
"default": -1, "default": -1,
"description": "Multiline field width - better to use CSS" "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 * Constructor
@ -126,6 +132,37 @@ var et2_textbox = et2_inputWidget.extend(
return this._super.apply(this, arguments); 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 * Set input widget size