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.

regexvarchar.py 941B

12345678910111213141516171819202122232425
  1. # -*- coding: utf-8 -*-
  2. import re
  3. from .varchar import EmDataField as VarcharDataField
  4. class EmDataField(VarcharDataField):
  5. help = 'String field validated with a regex. Takes two options : max_length and regex'
  6. ## @brief A string field validated by a regex
  7. # @param regex str : a regex string (passed as argument to re.compile())
  8. # @param max_length int : the max length for this field (default : 10)
  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(self.__class__, self).__init__(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" doesn\'t match the regex "%s"' % (value, self.regex))
  19. return (value, error)