0

I'm lost here and can't figure out what I'm missing, which is probably something stupid, but I need another set of eyes because as far as I can tell this should be working.

What I'm trying to do is allow my users to enter phone numbers in ways they are used to seeing, but then take that input and get a validated international phone number from Twilio and save it. By definition that means it will be in the following format - which is the format I need to have it in the database so that it interacts well with another part of the application:

+17085551212

I've debugged to the point there I know the values are coming in correctly, everything works right if I get an invalid number, etc. For some reason, the updated value is not being passed back to the form when I set form.cleaned_data['office_phone'] prior to the form.save(). So I am getting the original number (708) 555-1212 in the database.

forms.py

class ProfileForm(forms.ModelForm):
    office_phone = forms.CharField(max_length=20, label="Office Phone")

views.py

if form.is_valid():
    print (form.cleaned_data['office_phone'])
    pn = form.cleaned_data['office_phone'].replace(" ","")
    try:
        response = validator.phone_numbers.get(str(pn))
        form.cleaned_data['office_phone'] = str(response.phone_number)
        print form.cleaned_data
        form.save()
        success_message = "Your changes have been saved"
    except:
        error_message = "The contact phone number you entered is invalid."

console.output

(708) 555-1212
+17085551212
+17085551212
{'office_phone': '+17085551212'}
<tr><th><label for="id_office_phone">Office Phone:</label></th>
<td><input id="id_office_phone" maxlength="20" name="office_phone" type="text" value="(708) 555-1212" /></td></tr>

What am I missing here?

1 Answer 1

1

Made an edit: I realise that instead of overriding save, we should instead clean/validate the phone number by using custom validation:

class ProfileForm(forms.ModelForm):
    office_phone = forms.CharField(max_length=20, label="Office Phone")  

    def clean_office_phone(self):
        value = self.cleaned_data.get("office_phone")
        try:
            value = value.replace(" ", "")
            response = validator.phone_numbers.get(str(value))

        except:
            raise ValidationError("The contact phone number you entered is invalid")

        return str(response.phone_number)

views.py:

if form.is_valid():
        form.save()
        success_message = "Your changes have been saved"
Sign up to request clarification or add additional context in comments.

4 Comments

How will this handle an error case if the validator returns a 404 (invalid phone number) instead of coming back with a valid phone number? Also, not sure I understand the if commit: instance.save() return instance Tried it anyway and now it doesn't even seem to be saving changes. Yep, the save always fails. going to try troubleshooting it, but not sure.
Troublehooting the above and got it to work, but still don't understand why. Can you explain why this works here instead of just having the working code in the view?
Can you try the new edit? I have made major changes because I feel like the first answer was not the right way to go. Since you are "cleaning" and validating the phone number, we should use custom validation instead.
I have not tried what you edited your answer to - even though it may be the right way. I did get it working with the previous information you had in there. I don't know if you can, but I think you should put that back and then add this answer below it rather than replace the answer that worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.