Skip to content

Commit

Permalink
Fixed #4067 -- Fixed validation of IPAddressFields in newforms. Thank…
Browse files Browse the repository at this point in the history
…s to neils and the team in the Copenhagen sprint group.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6357 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
malcolmt committed Sep 16, 2007
1 parent c694587 commit c012b89
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
5 changes: 5 additions & 0 deletions django/db/models/fields/__init__.py
Expand Up @@ -880,6 +880,11 @@ def get_manipulator_field_objs(self):
def validate(self, field_data, all_data):
validators.isValidIPAddress4(field_data, None)

def formfield(self, **kwargs):
defaults = {'form_class': forms.IPAddressField}
defaults.update(kwargs)
return super(IPAddressField, self).formfield(**defaults)

class NullBooleanField(Field):
empty_strings_allowed = False
def __init__(self, *args, **kwargs):
Expand Down
10 changes: 9 additions & 1 deletion django/newforms/fields.py
Expand Up @@ -26,7 +26,7 @@
'RegexField', 'EmailField', 'FileField', 'ImageField', 'URLField', 'BooleanField',
'ChoiceField', 'NullBooleanField', 'MultipleChoiceField',
'ComboField', 'MultiValueField', 'FloatField', 'DecimalField',
'SplitDateTimeField',
'SplitDateTimeField', 'IPAddressField',
)

# These values, if given to to_python(), will trigger the self.required check.
Expand Down Expand Up @@ -635,3 +635,11 @@ def compress(self, data_list):
raise ValidationError(ugettext(u'Enter a valid time.'))
return datetime.datetime.combine(*data_list)
return None

ipv4_re = re.compile(r'^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$')

class IPAddressField(RegexField):
def __init__(self, *args, **kwargs):
RegexField.__init__(self, ipv4_re,
error_message=ugettext(u'Enter a valid IPv4 address.'),
*args, **kwargs)
11 changes: 10 additions & 1 deletion docs/newforms.txt
Expand Up @@ -1284,6 +1284,15 @@ When you use a ``FileField`` on a form, you must also remember to
Takes two optional arguments for validation, ``max_value`` and ``min_value``.
These control the range of values permitted in the field.

``IPAddressField``
~~~~~~~~~~~~~~~~~~

* Default widget: ``TextInput``
* Empty value: ``''`` (an empty string)
* Normalizes to: A Unicode object.
* Validates that the given value is a valid IPv4 address, using a regular
expression.

``MultipleChoiceField``
~~~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -1710,7 +1719,7 @@ the full list of conversions:
``ForeignKey`` ``ModelChoiceField`` (see below)
``ImageField`` ``ImageField``
``IntegerField`` ``IntegerField``
``IPAddressField`` ``CharField``
``IPAddressField`` ``IPAddressField``
``ManyToManyField`` ``ModelMultipleChoiceField`` (see
below)
``NullBooleanField`` ``CharField``
Expand Down
55 changes: 55 additions & 0 deletions tests/regressiontests/forms/tests.py
Expand Up @@ -3834,6 +3834,61 @@
>>> f.cleaned_data
{'field1': u'some text,JP,2007-04-25 06:24:00'}
# IPAddressField ##################################################################
>>> f = IPAddressField()
>>> f.clean('')
Traceback (most recent call last):
...
ValidationError: [u'This field is required.']
>>> f.clean(None)
Traceback (most recent call last):
...
ValidationError: [u'This field is required.']
>>> f.clean('127.0.0.1')
u'127.0.0.1'
>>> f.clean('foo')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid IPv4 address.']
>>> f.clean('127.0.0.')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid IPv4 address.']
>>> f.clean('1.2.3.4.5')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid IPv4 address.']
>>> f.clean('256.125.1.5')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid IPv4 address.']
>>> f = IPAddressField(required=False)
>>> f.clean('')
u''
>>> f.clean(None)
u''
>>> f.clean('127.0.0.1')
u'127.0.0.1'
>>> f.clean('foo')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid IPv4 address.']
>>> f.clean('127.0.0.')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid IPv4 address.']
>>> f.clean('1.2.3.4.5')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid IPv4 address.']
>>> f.clean('256.125.1.5')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid IPv4 address.']
#################################
# Tests of underlying functions #
#################################
Expand Down

0 comments on commit c012b89

Please sign in to comment.