Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Changed database PhoneNumberField to use USPhoneNumberField as its ne…

…wforms form field (instead of IntegerField)

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4556 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit abc949f58401f339e82328710e037e05acdbbae2 1 parent ec2eb4d
@adrianholovaty adrianholovaty authored
View
6 django/db/models/fields/__init__.py
@@ -748,6 +748,12 @@ def get_manipulator_field_objs(self):
def validate(self, field_data, all_data):
validators.isValidPhone(field_data, all_data)
+ def formfield(self, **kwargs):
+ from django.contrib.localflavor.usa.forms import USPhoneNumberField
+ defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
+ defaults.update(kwargs)
+ return USPhoneNumberField(**defaults)
+
class PositiveIntegerField(IntegerField):
def get_manipulator_field_objs(self):
return [oldforms.PositiveIntegerField]
View
16 tests/modeltests/model_forms/models.py
@@ -54,6 +54,13 @@ def save(self):
def __str__(self):
return self.headline
+class PhoneNumber(models.Model):
+ phone = models.PhoneNumberField()
+ description = models.CharField(maxlength=20)
+
+ def __str__(self):
+ return self.phone
+
__test__ = {'API_TESTS': """
>>> from django.newforms import form_for_model, form_for_instance, save_instance, BaseForm, Form, CharField
>>> import datetime
@@ -445,4 +452,13 @@ def __str__(self):
Traceback (most recent call last):
...
ValidationError: [u'Select a valid choice. 10 is not one of the available choices.']
+
+# PhoneNumberField ############################################################
+
+>>> PhoneNumberForm = form_for_model(PhoneNumber)
+>>> f = PhoneNumberForm({'phone': '(312) 555-1212', 'description': 'Assistance'})
+>>> f.is_valid()
+True
+>>> f.clean_data
+{'phone': u'312-555-1212', 'description': u'Assistance'}
"""}
Please sign in to comment.
Something went wrong with that request. Please try again.