Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#5847 closed (invalid)

Improvement for newforms CharField - accepting size HTML attribute

Reported by: danh@… Owned by: nobody
Component: Forms Version: master
Severity: Keywords: newforms input size
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

The newforms.fields.CharField doesn't accept HTML size attribute and as a result during rendering all fields get the same length.
This is also the cause for #5609.

It would be very easy to modify it to accept it. Right now I am subclassing CharField to add this functionality but it would be more convenient to have it in the framework.
This is what I'm using right now:

import django.newforms as forms

class CharField(forms.fields.CharField):
    def __init__(self, size=None, *args, **kwargs):
        self.size = size
        super(CharField, self).__init__(*args, **kwargs)

    def widget_attrs(self, widget):
        attrs = super(forms.fields.CharField, self).widget_attrs(widget)
        if self.max_length is not None and isinstance(widget, (forms.fields.TextInput, forms.fields.PasswordInput)):
            attrs['size'] = str(self.size)
        return attrs

Change History (3)

comment:1 Changed 7 years ago by brosner

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

Fields are not responsible for displaying themselves. That is the job of its widget. The size attribute must be passed into the widget when it is created.

comment:2 Changed 7 years ago by brosner

My apologizes I should have provided a link to the documentation that covers this.

http://www.djangoproject.com/documentation/newforms/#widgets

Read through that section and you will discover a better method of dealing with the size attribute (the correct way)

comment:3 Changed 7 years ago by danh@…

Thanks, you're right.
Much better now.

Note: See TracTickets for help on using tickets.
Back to Top