Reputation: 18092
I am trying to do a PhoneField that convert the value as a standardized value.
In this case, I want to use this clean method.
def clean(self):
phone = self.cleaned_data.get('phone')
# Is it already standardized ?
if phone.startswith('+'):
mo = re.search(r'^\+\d{2,3}\.\d{9,11}$', phone)
if not mo:
raise forms.ValidationError(_(u'Vous devez entrer un numéro de téléphone. (+33.389520638 ou 0389520638).'))
else:
return phone
# If not, it must be a french number.
phone = re.sub("\D", "", phone) # Suppression des caractères séparateurs
mo = re.search(r'^\d{10}$', phone) # Numéro à 10 chiffres
if not mo:
raise forms.ValidationError(_(u'Vous devez entrer un numéro de téléphone. (+33.389520638 ou 0389520638).'))
else:
phone = mo.group()[-9:]
return u'+33.%s' % phone
If I use it in a form, it works well. But I would like to use it as a form field.
I tried to do like that :
EMPTY_VALUES = (None, '')
class PhoneInput (forms.TextInput):
def render(self, name, value, attrs=None):
if value not in EMPTY_VALUES:
value = phone_render(value)
else:
value = None
return super(PhoneInput, self).render(name, value, attrs)
class PhoneField(models.CharField):
widget = PhoneInput
def __init__(self, *args, **kwargs):
kwargs['max_length'] = 16
super(PhoneField, self).__init__(*args, **kwargs)
def get_internal_type(self):
return "CharField"
def clean(self, value):
phone = super(PhoneField, self).clean(value)
# Le numéro contient-il un indicatif ?
if phone.startswith('+'):
mo = re.search(r'^\+\d{2,3}\.\d{9,11}$', phone)
if not mo:
raise forms.ValidationError(_(u'Vous devez entrer un numéro de téléphone. (+33.389520638 ou 0389520638).'))
else:
return phone
# Pas d'indicatif : on est en France par défaut
phone = re.sub("\D", "", phone) # Suppression des caractères séparateurs
mo = re.search(r'^\d{10}$', phone) # Numéro à 10 chiffres
if not mo:
raise forms.ValidationError(_(u'Vous devez entrer un numéro de téléphone. (+33.389520638 ou 0389520638).'))
else:
phone = mo.group()[-9:]
return u'+33.%s' % phone
But the clean method is never called. Can you help me ?
Upvotes: 1
Views: 554
Reputation: 18092
Here is how I fixed it using becomingGuru help :)
class PhoneFormField(forms.CharField):
widget = PhoneInput
def clean(self, value):
phone = super(PhoneFormField, self).clean(value)
# Le numéro contient-il un indicatif ?
if phone.startswith('+'):
mo = re.search(r'^\+\d{2,3}\.\d{9,11}$', phone)
if not mo:
raise forms.ValidationError(_(u'Vous devez entrer un numéro de téléphone. (+33.389520638 ou 0389520638).'))
else:
return phone
# Pas d'indicatif : on est en France par défaut
phone = re.sub("\D", "", phone) # Suppression des caractères séparateurs
mo = re.search(r'^\d{10}$', phone) # Numéro à 10 chiffres
if not mo:
raise forms.ValidationError(_(u'Vous devez entrer un numéro de téléphone. (+33.389520638 ou 0389520638).'))
else:
phone = mo.group()[-9:]
return u'+33.%s' % phone
class PhoneField(models.CharField):
def __init__(self, *args, **kwargs):
kwargs['max_length'] = 16
super(PhoneField, self).__init__(*args, **kwargs)
def get_internal_type(self):
return "CharField"
def formfield(self, form_class=PhoneFormField, **kwargs):
return super(PhoneField, self).formfield(form_class=form_class, **kwargs)
Thank you for your help.
Upvotes: 1
Reputation: 87077
You are mixing up the model fields and form fields.
Form Fields need to be first defined and then corresponding model Fields need to be asked to use those form fields for a model form.
See specifying-the-form-field-for-a-model-field documentation
Basically you need to define a method called formfield on the model field
def formfield(self, **kwargs):
# This is a fairly standard way to set up some defaults
# while letting the caller override them.
defaults = {'form_class': MyFormField}
defaults.update(kwargs)
return super(HandField, self).formfield(**defaults)
The Field that you have created above, has a clean method, that is a requirement for a form field, not a model field
So, now you should define a form field (the one you have defined, except extend it from forms.CharField
, not models.CharField
) and then define a model field, as per model field creation definitions, and include the method like above.
Alternatively and simplistically, you could just choose to define a form field, and in the model form, override the default form field of this particular modelfield. (Altho' in this case validation doesnt take place unless data is entered from that modelform)
Upvotes: 6