Opened 17 years ago

Closed 17 years ago

Last modified 17 years ago

#5847 closed (invalid)

Improvement for newforms CharField - accepting size HTML attribute

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

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 by Brian Rosner, 17 years ago

Resolution: invalid
Status: newclosed

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 by Brian Rosner, 17 years ago

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 by danh@…, 17 years ago

Thanks, you're right.
Much better now.

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