Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent having "None" in the placeholder #591

Merged
merged 4 commits into from
Feb 25, 2020
Merged

Prevent having "None" in the placeholder #591

merged 4 commits into from
Feb 25, 2020

Conversation

Benbb96
Copy link
Contributor

@Benbb96 Benbb96 commented Feb 10, 2020

I had None set as a placeholder on my ModelSelect2MultipleWidget because I initialize the widget through the Meta class of my form and I don't explicitly set the empty_label on my field.

So I think it should check if the empty_label is set before assigning it to the placeholder.

This issue is linked to #565

@codecov-io
Copy link

codecov-io commented Feb 10, 2020

Codecov Report

Merging #591 into master will not change coverage by %.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #591   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            2         2           
  Lines            8         8           
=========================================
  Hits             8         8           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 39ab326...d042241. Read the comment docs.

@Benbb96
Copy link
Contributor Author

Benbb96 commented Feb 10, 2020

Maybe the change should happen in line 83 :
default_attrs['data-placeholder'] = self.empty_label if self.empty_label or ''

@Benbb96 Benbb96 marked this pull request as ready for review February 10, 2020 15:31
@codingjoe
Copy link
Collaborator

@Benbb96 thanks for the contribution. I am not quite certain about this change though. Why are is your fields empty_label None instead of ""? Wouldn't it be more precise to set the fields label to whatever should be displayed? I will close the PR for now and repoen it if need be, ok?

@codingjoe codingjoe closed this Feb 13, 2020
@Benbb96
Copy link
Contributor Author

Benbb96 commented Feb 13, 2020

Okay so here is an example of my code when None appears in the placeholder :

class OrderForm(forms.ModelForm):
    class Meta:
        model = Order
        fields = ('status', 'customer_contacts')
        widgets = {
            'status': forms.Select(attrs={'class': 'form-control'}),
            'customer_contacts': ModelSelect2MultipleWidget(
                model=User,
                search_fields=['username__icontains', 'first_name__icontains', 'last_name__icontains']
            ),
        }

bug_select2_none

And next is the way I have it working without applying the patch I submitted :

class OrderForm(forms.ModelForm):
    customer_contacts = forms.ModelMultipleChoiceField(
        queryset=User.objects.all(),
        widget=ModelSelect2MultipleWidget(
            model=User,
            search_fields=['username__icontains', 'first_name__icontains', 'last_name__icontains']
        )
    )

    class Meta:
        model = Order
        fields = ('status', 'customer_contacts')
        widgets = {
            'status': forms.Select(attrs={'class': 'form-control'})
        }

Why do I have to explicitly override the field customer_contacts to make it work ?

Note : I didn't have this issue before this commit.

@codingjoe
Copy link
Collaborator

codingjoe commented Feb 13, 2020

Hi @Benbb96,

thanks for responding so quickly. I had a look at the Django code, to figure out why the label is None. That seem to be the case when a field is not required, which I honestly was not aware of. Anyhow, I believe the reason why you are seeing None is placeholder of the placeholder.

default_attrs['data-placeholder'] = self.empty_label

Can you test if replacing the line with

default_attrs['data-placeholder'] = self.empty_label or ""

solves your problem?

If so, I am happy to accept this as a patch.

Best
-Joe

@Benbb96
Copy link
Contributor Author

Benbb96 commented Feb 14, 2020

Hi @codingjoe,

You're right, if I set my field as required, then the placeholder isn't None.

I've tested with your proposal and it also works so I updated my code on my fork.
Should I make another Pull Request ?

@codingjoe
Copy link
Collaborator

Just update it and maybe add test if possible. Ping me when you are done and I'll review an release it 👍

@Benbb96
Copy link
Contributor Author

Benbb96 commented Feb 20, 2020

Hi @codingjoe

I added a test and pushed it onto my fork, but I'm not sure if you are able to see it from here since the pull request is closed ?

I've added my test into the test_empty_option function (in test_forms.py) because I wanted to use the AlbumSelect2MultipleWidgetForm which has the non required field featured_artists and produces the bug without my patch.
Maybe you prefer to put the test in a separate function ?

@codingjoe codingjoe reopened this Feb 20, 2020
@codingjoe codingjoe merged commit e7fa49a into applegrew:master Feb 25, 2020
@codingjoe
Copy link
Collaborator

codingjoe commented Feb 25, 2020

Released in 7.2.1 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants