Reputation: 14280
I'm building a Django form that includes a phone number field. I've been referring to these two SO questions to understand how to do it: 1, 2. I've created this form field:
class ContactForm(forms.Form):
phone = forms.RegexField(
regex = r'^\+?[1-9]\d{1,14}$',
#regex = r'\+?\d{10,14}$',
error_messages = {'required', 'Phone number required'},
widget = forms.TextInput(attrs={'class': 'form-control'})
)
I display the field in my template:
<div>
<label for="id_phone">Your Phone Number</label>
{{ form.phone.errors }}
{{ form.phone }}
</div>
I understand what the regexes are doing and they look correct to me. However, I'm getting this error if I use either one of them:
ValueError at /business/contact/
dictionary update sequence element #0 has length 8; 2 is required
...
Exception Location: /srv/http/example.com/venvs/dev/local/lib/python2.7/site-packages/django/forms/fields.py in __init__, line 125
(stacktrace...)
widget = forms.TextInput(attrs={'class': 'form-control'})
super(RegexField, self).__init__(max_length, min_length, *args, **kwargs)
super(CharField, self).__init__(*args, **kwargs)
messages.update(error_messages or {})
(end of stacktrace)
Can anyone see what's causing this error? It seems to be caused by the regexes.
Upvotes: 1
Views: 1200
Reputation: 14280
I just discovered the error. It's in this line:
error_messages = {'required', 'Phone number required'},
I needed to replace the "," with a ":":
error_messages = {'required': 'Phone number required'},
Upvotes: 2