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

Already on GitHub? Sign in to your account

Pass the maxlength attribute from models.TextField to forms.CharField (#22206) #2391

Closed
wants to merge 3 commits into
from

Conversation

Projects
None yet
2 participants
Contributor

qris commented Mar 4, 2014

Pass this to the widget as the maxlength attribute, regardless of its class,
so that it works on Textarea and any other widget that you might choose to
use for a CharField.

See https://code.djangoproject.com/ticket/22206 for more details.

Member

claudep commented Mar 4, 2014

I've just pushed separately in 97a5971 the attibute/attribute typo, thanks!

@claudep claudep commented on an outdated diff Mar 4, 2014

tests/forms_tests/tests/test_forms.py
@@ -314,6 +314,20 @@ class ContactForm(Form):
self.assertHTMLEqual(f['message'].as_text(), '<input type="text" name="message" value="I love you." />')
self.assertHTMLEqual(f['message'].as_hidden(), '<input type="hidden" name="message" value="I love you." />')
+ # CharField should set the maxlength attribute on the widget
+ self.assertHTMLEqual(CharField(max_length=None).widget.render('cats', None),
+ '<input type="text" name="cats" />')
+ self.assertHTMLEqual(CharField(max_length=1234).widget.render('cats', None),
+ '<input type="text" name="cats" maxlength=1234 />')
+
+ # Even if the widget is a Textarea rather than a TextInput
+ self.assertHTMLEqual(CharField(max_length=None,
+ widget=Textarea).widget.render('cats', None),
+ '<textarea rows="10" cols="40" name="cats" />')
+ self.assertHTMLEqual(CharField(max_length=1234,
+ widget=Textarea).widget.render('cats', None),
+ '<textarea rows="10" cols="40" name="cats" maxlength=1234 />')
+
@claudep

claudep Mar 4, 2014

Member

I'm not totally convinced these added tests are useful here. I'd add one more line in test_fields.py (self.assertEqual(f.widget_attrs(Textarea()), {'maxlength': '10'}) and remove these.

Member

claudep commented Mar 4, 2014

Also pushed the class name spelling fix. You should be able to rebase and force push your branch.

Member

claudep commented Mar 4, 2014

Oh, and it would be nice to add a note in the minor features in the 1.7 release notes (Forms section).

qris added some commits Mar 4, 2014

@qris qris Pass the maxlength attribute from models.TextField to forms.CharField.
Pass this to the widget as the maxlength attribute, regardless of its class,
so that it works on Textarea and any other widget that you might choose to
use for a CharField.
c4cc3b1
@qris qris Add test that CharField sets maxlength attr on Textarea.
Remove HTML output tests as requested by @claudep.
386a891
@qris qris Document the addition of `maxlength` attributes to <textarea> tags. 78b63eb
Contributor

qris commented Mar 4, 2014

Thanks @claudep, I hope I've addressed these issues.

Member

claudep commented Mar 5, 2014

Thanks, pushed in 95c74b9

@claudep claudep closed this Mar 5, 2014

@qris qris deleted the aptivate:ticket_22206 branch Mar 5, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment