diff --git a/django/forms/fields.py b/django/forms/fields.py
index 8e727b4..145c2dc 100644
a
|
b
|
class RegexField(CharField):
|
441 | 441 | error_messages['invalid'] = error_message |
442 | 442 | kwargs['error_messages'] = error_messages |
443 | 443 | super(RegexField, self).__init__(max_length, min_length, *args, **kwargs) |
| 444 | self._set_regex(regex) |
| 445 | |
| 446 | def _get_regex(self): |
| 447 | return self._regex |
| 448 | |
| 449 | def _set_regex(self, regex): |
444 | 450 | if isinstance(regex, basestring): |
445 | 451 | regex = re.compile(regex) |
446 | | self.regex = regex |
447 | | self.validators.append(validators.RegexValidator(regex=regex)) |
| 452 | self._regex = regex |
| 453 | if hasattr(self, '_regex_validator') and self._regex_validator in self.validators: |
| 454 | self.validators.remove(self._regex_validator) |
| 455 | self._regex_validator = validators.RegexValidator(regex=regex) |
| 456 | self.validators.append(self._regex_validator) |
| 457 | |
| 458 | regex = property(_get_regex, _set_regex) |
448 | 459 | |
449 | 460 | class EmailField(CharField): |
450 | 461 | default_error_messages = { |
diff --git a/tests/regressiontests/forms/tests/fields.py b/tests/regressiontests/forms/tests/fields.py
index d37ad5e..50822fa 100644
a
|
b
|
class FieldsTests(SimpleTestCase):
|
489 | 489 | self.assertRaisesMessage(ValidationError, "[u'Ensure this value has at most 10 characters (it has 11).']", f.clean, '12345678901') |
490 | 490 | self.assertRaisesMessage(ValidationError, "[u'Enter a valid value.']", f.clean, '12345a') |
491 | 491 | |
| 492 | def test_change_regex_after_init(self): |
| 493 | f = RegexField('^[a-z]+$') |
| 494 | f.regex = '^\d+$' |
| 495 | self.assertEqual(u'1234', f.clean('1234')) |
| 496 | self.assertRaisesMessage(ValidationError, "[u'Enter a valid value.']", f.clean, 'abcd') |
| 497 | |
492 | 498 | # EmailField ################################################################## |
493 | 499 | |
494 | 500 | def test_emailfield_1(self): |