Python django.forms.RegexField() Examples

The following are 17 code examples for showing how to use django.forms.RegexField(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module django.forms , or try the search function .

Example 1
Project: djongo   Author: nesdis   File: test_regexfield.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def test_regexfield_4(self):
        f = RegexField('^[0-9]+$', min_length=5, max_length=10)
        with self.assertRaisesMessage(ValidationError, "'Ensure this value has at least 5 characters (it has 3).'"):
            f.clean('123')
        with self.assertRaisesMessage(
            ValidationError,
            "'Ensure this value has at least 5 characters (it has 3).', "
            "'Enter a valid value.'",
        ):
            f.clean('abc')
        self.assertEqual('12345', f.clean('12345'))
        self.assertEqual('1234567890', f.clean('1234567890'))
        with self.assertRaisesMessage(ValidationError, "'Ensure this value has at most 10 characters (it has 11).'"):
            f.clean('12345678901')
        with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
            f.clean('12345a') 
Example 2
Project: djongo   Author: nesdis   File: test_regexfield.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def test_regexfield_4(self):
        f = RegexField('^[0-9]+$', min_length=5, max_length=10)
        with self.assertRaisesMessage(ValidationError, "'Ensure this value has at least 5 characters (it has 3).'"):
            f.clean('123')
        with self.assertRaisesMessage(
            ValidationError,
            "'Ensure this value has at least 5 characters (it has 3).', "
            "'Enter a valid value.'",
        ):
            f.clean('abc')
        self.assertEqual('12345', f.clean('12345'))
        self.assertEqual('1234567890', f.clean('1234567890'))
        with self.assertRaisesMessage(ValidationError, "'Ensure this value has at most 10 characters (it has 11).'"):
            f.clean('12345678901')
        with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
            f.clean('12345a') 
Example 3
Project: wagtail   Author: wagtail   File: field_block.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, regex, required=True, help_text=None, max_length=None, min_length=None,
                 error_messages=None, validators=(), *args, **kwargs):
        self.field = forms.RegexField(
            regex=regex,
            required=required,
            help_text=help_text,
            max_length=max_length,
            min_length=min_length,
            error_messages=error_messages,
            validators=validators,
        )
        super().__init__(*args, **kwargs) 
Example 4
Project: django-mysql   Author: adamchainz   File: test_forms.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_validators_fail(self):
        field = SimpleListField(forms.RegexField("[a-e]{2}"))
        with pytest.raises(exceptions.ValidationError) as excinfo:
            field.clean("a,bc,de")
        assert (
            excinfo.value.messages[0]
            == "Item 1 in the list did not validate: Enter a valid value."
        ) 
Example 5
Project: django-mysql   Author: adamchainz   File: test_forms.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_validators_fail(self):
        field = SimpleSetField(forms.RegexField("[a-e]{2}"))
        with pytest.raises(exceptions.ValidationError) as excinfo:
            field.clean("a,bc,de")
        assert (
            excinfo.value.messages[0]
            == 'Item "a" in the set did not validate: Enter a valid value.'
        ) 
Example 6
Project: graphene-django   Author: graphql-python   File: test_converter.py    License: MIT License 5 votes vote down vote up
def test_should_regex_convert_string():
    assert_conversion(forms.RegexField, String, "[0-9]+") 
Example 7
Project: djongo   Author: nesdis   File: test_regexfield.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_regexfield_1(self):
        f = RegexField('^[0-9][A-F][0-9]$')
        self.assertEqual('2A2', f.clean('2A2'))
        self.assertEqual('3F3', f.clean('3F3'))
        with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
            f.clean('3G3')
        with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
            f.clean(' 2A2')
        with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
            f.clean('2A2 ')
        with self.assertRaisesMessage(ValidationError, "'This field is required.'"):
            f.clean('') 
Example 8
Project: djongo   Author: nesdis   File: test_regexfield.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_regexfield_2(self):
        f = RegexField('^[0-9][A-F][0-9]$', required=False)
        self.assertEqual('2A2', f.clean('2A2'))
        self.assertEqual('3F3', f.clean('3F3'))
        with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
            f.clean('3G3')
        self.assertEqual('', f.clean('')) 
Example 9
Project: djongo   Author: nesdis   File: test_regexfield.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_regexfield_3(self):
        f = RegexField(re.compile('^[0-9][A-F][0-9]$'))
        self.assertEqual('2A2', f.clean('2A2'))
        self.assertEqual('3F3', f.clean('3F3'))
        with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
            f.clean('3G3')
        with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
            f.clean(' 2A2')
        with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
            f.clean('2A2 ') 
Example 10
Project: djongo   Author: nesdis   File: test_regexfield.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_regexfield_unicode_characters(self):
        f = RegexField(r'^\w+$')
        self.assertEqual('éèøçÎÎ你好', f.clean('éèøçÎÎ你好')) 
Example 11
Project: djongo   Author: nesdis   File: test_array.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_validators_fail(self):
        field = SimpleArrayField(forms.RegexField('[a-e]{2}'))
        with self.assertRaises(exceptions.ValidationError) as cm:
            field.clean('a,bc,de')
        self.assertEqual(cm.exception.messages[0], 'Item 1 in the array did not validate: Enter a valid value.') 
Example 12
Project: djongo   Author: nesdis   File: test_regexfield.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_regexfield_1(self):
        f = RegexField('^[0-9][A-F][0-9]$')
        self.assertEqual('2A2', f.clean('2A2'))
        self.assertEqual('3F3', f.clean('3F3'))
        with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
            f.clean('3G3')
        with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
            f.clean(' 2A2')
        with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
            f.clean('2A2 ')
        with self.assertRaisesMessage(ValidationError, "'This field is required.'"):
            f.clean('') 
Example 13
Project: djongo   Author: nesdis   File: test_regexfield.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_regexfield_2(self):
        f = RegexField('^[0-9][A-F][0-9]$', required=False)
        self.assertEqual('2A2', f.clean('2A2'))
        self.assertEqual('3F3', f.clean('3F3'))
        with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
            f.clean('3G3')
        self.assertEqual('', f.clean('')) 
Example 14
Project: djongo   Author: nesdis   File: test_regexfield.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_regexfield_3(self):
        f = RegexField(re.compile('^[0-9][A-F][0-9]$'))
        self.assertEqual('2A2', f.clean('2A2'))
        self.assertEqual('3F3', f.clean('3F3'))
        with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
            f.clean('3G3')
        with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
            f.clean(' 2A2')
        with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
            f.clean('2A2 ') 
Example 15
Project: djongo   Author: nesdis   File: test_regexfield.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_change_regex_after_init(self):
        f = RegexField('^[a-z]+$')
        f.regex = '^[0-9]+$'
        self.assertEqual('1234', f.clean('1234'))
        with self.assertRaisesMessage(ValidationError, "'Enter a valid value.'"):
            f.clean('abcd') 
Example 16
Project: djongo   Author: nesdis   File: test_array.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_validators_fail(self):
        field = SimpleArrayField(forms.RegexField('[a-e]{2}'))
        with self.assertRaises(exceptions.ValidationError) as cm:
            field.clean('a,bc,de')
        self.assertEqual(cm.exception.messages[0], 'Item 1 in the array did not validate: Enter a valid value.') 
Example 17
Project: esdc-ce   Author: erigones   File: fields.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, regex, max_length=None, min_length=None, *args, **kwargs):
        super(RegexField, self).__init__(max_length, min_length, *args, **kwargs)
        self.regex = regex