How to change django-phonenumber-field error messages

3k Views Asked by At

I am using django-phonenumber-field.

Everyone who uses this app probably knows that any prefix has the same example "Enter a valid phone number (e.g. +12125552368)."

Enter a valid phone number (e.g. +12125552368).

So, I don't like it and I wanted to change it but I could not change this text, I thought I would change it in html like this:

    {% if form.phone.errors %}
      
        <p class="alert-p"></p>Incorrect International Calling Code or Mobile Number!</p>
     
    {% endif %}

Incorrect International Calling Code or Mobile Number!

But when I changed it I realized that this method would not work for me because there was a likelihood that users would enter the same phone number and could not understand why it would not save if it did not send the appropriate error message. (phone number is unique) Now there is a problem, in one case, when the user enters the wrong number in the prefix, this time I want my built-in text to drop out as an error message, but when users enter the same number I want to drop out the django-phonenumber-field default Error message Account with this Phone already exists.

I will take any advice on how I can solve this problem.

forms.py

    from django import forms
    from django.contrib.auth.forms import ReadOnlyPasswordHashField
    from django.db.models.fields import CharField
    from django.forms import widgets
    from phonenumber_field.formfields import PhoneNumberField
    from phonenumber_field.widgets import PhoneNumberPrefixWidget
    from .models import Account
    from django_countries.widgets import CountrySelectWidget
    from django import forms
    from phonenumber_field.formfields import PhoneNumberField
    

class UserChangeForm(forms.ModelForm):

    class Meta:
        model = Account
        fields = ('email', 'fname', 'lname', 'phone','bday', 'country', 'gender','picture')
        
    # email = forms.CharField(widget=forms.EmailInput(attrs={'class':'form-control'}))
    # fname = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control','placeholder':'Name'}))
    # lname = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control', 'placeholder':'Surname'}))
    phone = PhoneNumberField(widget=PhoneNumberPrefixWidget(initial='GE'))
    # bday = forms.CharField(widget=DatePickerInput(attrs={'class':'form-control', 'placeholder':'Birth Date'}))
    # country = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control','placeholder':'Country'}))
    # gender = forms.ChoiceField(choices=Account.GENDER_CHOICES, widget=forms.Select(attrs={'class':'regDropDown'}))
    # picture = forms.FileField(widget=forms.ClearableFileInput(attrs={'class':'form-control'}))


    
    
    def clean_password(self):
        # Regardless of what the user provides, return the initial value.
        # This is done here, rather than on the field, because the
        # field does not have access to the initial value
        return self.initial["password"]
1

There are 1 best solutions below

10
On BEST ANSWER

In the form, you can override the error message by overriding it in the phone_number.error_messages dictionary:

from django import forms
from phonenumber_field.formfields import PhoneNumberField

class UserChangeForm(forms.ModelForm):
    # …
    phone = PhoneNumberField(widget=PhoneNumberPrefixWidget(initial='GE'))
    phone.error_messages['invalid'] = 'Incorrect International Calling Code or Mobile Number!'
    # …