Module: check_mk
Branch: master
Commit: 27fc195746f14e6f0d8b91f4cd135a61e761583c
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=27fc195746f14e…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Tue Jan 10 12:04:33 2012 +0100
ValueSpec: Allow regex in all TextInputs
---
web/htdocs/valuespec.py | 13 +++++++------
1 files changed, 7 insertions(+), 6 deletions(-)
diff --git a/web/htdocs/valuespec.py b/web/htdocs/valuespec.py
index e0bfcef..669beb7 100644
--- a/web/htdocs/valuespec.py
+++ b/web/htdocs/valuespec.py
@@ -232,6 +232,9 @@ class TextAscii(ValueSpec):
ValueSpec.__init__(self, **kwargs)
self._size = kwargs.get("size", 30)
self._allow_empty = kwargs.get("allow_empty", True)
+ self._regex = kwargs.get("regex")
+ if type(self._regex) == str:
+ self._regex = re.compile(self._regex)
def canonical_value(self):
return ""
@@ -252,21 +255,19 @@ class TextAscii(ValueSpec):
def validate_value(self, value, varprefix):
if not self._allow_empty and value.strip() == "":
raise MKUserError(varprefix, _("An empty value is not allowed
here."))
+ if value and self._regex:
+ if not self._regex.match(value):
+ raise MKUserError(varprefix, _("Your input odes not match the
required format."))
class EmailAddress(TextAscii):
def __init__(self, **kwargs):
TextAscii.__init__(self, **kwargs)
- self._re_email = re.compile('^[A-Z0-9._%+-]+(a)[A-Z0-9.-]+\.[A-Z]{2,4}$'4}$',
re.I)
+ self._regex = re.compile('^[A-Z0-9._%+-]+(a)[A-Z0-9.-]+\.[A-Z]{2,4}$'4}$',
re.I)
def value_to_text(self, value):
return '<a href="mailto:%s">%s</a>' % (value,
value)
- def validate_value(self, value, varprefix):
- TextAscii.validate_value(self, value, varprefix)
- if value:
- if not self._re_email.match(value):
- raise MKUserError(varprefix, _("You entered an invalid email
address."))
class TextUnicode(TextAscii):
def __init__(self, **kwargs):