question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PhoneNumberPrefixWidget, problem with 'required'

See original GitHub issue

Hi,

Thanks for django-phonenumber-field

When I am using django-phonenumber-field widget with required=True , it works fine but if I change to required=False then also it is not allow to submit empty.

What I found is in class PhoneNumberPrefixWidget and in method ‘value_from_datadict’, even when there is no data it will return dot(.) and that will be raise an validation error

If I change like this, then it works

def value_from_datadict(self, data, files, name):
        values = super(PhoneNumberPrefixWidget, self).value_from_datadict(
            data, files, name)
        if all(values):
            return '%s.%s' % tuple(values)
        return ''

But if you try above code with required=True then it will throw error because there is no required message is set but this can be set on form level for now, by setting error_messages

Thanks

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
archatascommented, Aug 24, 2016

I also experienced the same problem and solved it temporarily by extending the PhoneNumberPrefixWidget and overwriting the value_from_datadict method in my code like this:

from phonenumber_field.widgets import PhoneNumberPrefixWidget as PhoneNumberPrefixWidgetBase

class PhoneNumberPrefixWidget(PhoneNumberPrefixWidgetBase):
    def value_from_datadict(self, data, files, name):
        values = super(PhoneNumberPrefixWidgetBase, self).value_from_datadict(
            data, files, name)
        if all(values):
            return '%s.%s' % tuple(values)
        return ""

0reactions
francoisfreitagcommented, Aug 26, 2022

Should be resolved with https://github.com/stefanfoulis/django-phonenumber-field/pull/512, available in the next release.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Django PhoneNumberField library giving problem when using ...
But when I access the field and give bootstrap class, the layout changes. It displays PhoneNumberPrefixWidget in one line and PhoneNumberField ...
Read more >
django-phonenumber-field - Bountysource
I had problems getting a form with the widget to work. ... How do I style the PhoneNumberPrefixWidget since it uses multi widget...
Read more >
10 Django tutorials | Setup International Phone number widget ...
An error occurred while retrieving sharing information. Please try again later. 0:00. 0:00 / 16:56•Watch full video
Read more >
Display a MultiWidget Form manually in template - Reddit
The issue is in the PhoneNumberField + PhoneNumberPrefixWidget. ... After I have learned Django for Web Backends, I need suggestions for ...
Read more >
django-phonenumber-field - PyPI
Skip to main content. Switch to mobile version. Warning Some features may not work without JavaScript. Please try enabling it if you encounter...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found