No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

regexchar.py 904B

123456789101112131415161718192021222324252627
  1. #-*- coding: utf-8 -*-
  2. import re
  3. from . import char
  4. class EmFieldType(char.EmFieldType):
  5. help = 'String field validated with a regex. Take two options : max_length and regex'
  6. ## @brief A char field validated with a regex
  7. # @param regex str : a regex string (passed as argument to re.compile() )
  8. # @param max_length int : the maximum length for this field
  9. # @param **kwargs
  10. def __init__(self, regex='', max_length=10, **kwargs):
  11. self.regex = regex
  12. self.compiled_re = re.compile(regex) # trigger an error if invalid regex
  13. super().__init__(check_data_value=check_value, max_length=max_length, **kwargs)
  14. def _check_data_value(self,value):
  15. error = None
  16. if not self.compiled_re.match(value):
  17. value = ''
  18. error = TypeError('"%s" don\'t match the regex "%s"' % (value, self.regex))
  19. return (value, error)