1

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"]
ATISE
  • 34
  • 1
  • 8

1 Answers1

0

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!'
    # …
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • Thanks for the answer, if you do not mind, maybe write to me in more detail, I wrote in the form what you wrote to me, but nothing has changed, should I write something in views.py? I have not been studying Django for a long time and I may not understand simple things. – ATISE Jul 04 '21 at 20:14
  • @ATISE: can you share the current form you are using? [Edit](https://stackoverflow.com/posts/68248311/edit) the question. Can you specify what version of the `django-phonenumber-field` you are using? – Willem Van Onsem Jul 04 '21 at 20:15
  • I have added forms.py in my post. I am using "django-phonenumber-field 0.2a1" this version – ATISE Jul 04 '21 at 20:23
  • @ATISE: you should apply it to the `Form` you are using, so in this case the `UserChangeForm`. – Willem Van Onsem Jul 04 '21 at 20:23
  • 1
    It works now. Thank you so much for the help, you are a diamond. I have been trying to solve this problem all day. – ATISE Jul 04 '21 at 20:32
  • I will also ask one question, is it possible to make this field no required? – ATISE Jul 04 '21 at 20:44
  • @ATISE: set the model field to `blank=True`, and the form field as `phone = PhoneNumberField(widget=PhoneNumberPrefixWidget(initial='GE'), required=False)`. – Willem Van Onsem Jul 04 '21 at 20:52
  • I have tried it before I asked but I got error >> TypeError: __init__() got an unexpected keyword argument 'required'. But I have done it in models.py: phone = PhoneNumberField(blank=True, null=True, unique=True, required=False) – ATISE Jul 04 '21 at 21:25
  • @ATISE: no, `required` is *not* for the model field, for the model field it is simply `blank=True`, for the *form field*, it is `required=False` (and *without* the `blank=True` part). – Willem Van Onsem Jul 04 '21 at 21:29