# # RegexField for Formulator # # Copyright (c) 2003 Philipp von Weitershausen # # import os, re from Products.Formulator.DummyField import fields from Products.Formulator.Field import ZMIField from Products.Formulator.FieldRegistry import FieldRegistry from Products.Formulator.Validator import StringBaseValidator from Products.Formulator import Widget class RegexValidator(StringBaseValidator): property_names = StringBaseValidator.property_names + \ ['regex'] regex = fields.StringField( 'regex', title="Regular Expression", required=1, default="", description="Enter a regular expression here" ) message_names = StringBaseValidator.message_names + \ ['regex_not_matched'] regex_not_matched = "The entered value did not match the regular " \ "expression." def validate(self, field, key, REQUEST): value = REQUEST.get(key, "") srematch = re.match(field.get_value('regex'), value) if srematch is None: self.raise_error('regex_not_matched', field) return value RegexValidatorInstance = RegexValidator() class RegexField(ZMIField): meta_type = "RegexField" widget = Widget.TextWidgetInstance validator = RegexValidatorInstance path = os.path.dirname(__file__) iconpath = os.path.join(path, "RegexField.gif") FieldRegistry.registerField(RegexField, icon=iconpath)