Code

Opened 7 years ago

Closed 7 years ago

#3578 closed (fixed)

newforms EmailField takes the wrong max_length when rendered as html and a CharField exists.

Reported by: Georgi Stanojevski <glisha gmail com> Owned by: alagu
Component: Forms Version: master
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: yes
Easy pickings: UI/UX:

Description

If I have a form with CharField and a form with EmailField like bellow. The TextInput widget in the EmailField takes the max_length attribute from the prevous form and displays the input html with a wrong attribute. The validation of the field is OK.

I guess this happens in the TextInput widget, but I can't figure it where on my own to help.

from django import newforms as forms
from django.newforms.widgets import *

attrs1={'class':'aclass'}

class form1(forms.Form):
    field1 = forms.CharField(widget=TextInput(attrs=attrs1),max_length=30)

class form2(forms.Form):
    field2 = forms.EmailField(widget=TextInput(attrs=attrs1),max_length=10)

f2 = form2()

f2.as_ul()
Out[12]: u'<li><label for="id_field2">Field2:</label> <input id="id_field2" type="text" class="aclass" name="field2" maxlength="30" /></li>'

f2 = form2({'field2':'over10@domain.com'})

f2.is_valid()
Out[9]: False

In [10]: f2.errors
Out[10]: {'field2': [u'Ensure this value has at most 10 characters.']

Attachments (1)

3578_newforms_emailfield_maxlength_fix.diff (571 bytes) - added by Brian Rosner <brosner@…> 7 years ago.
patch to fix #3578

Download all attachments as: .zip

Change History (8)

comment:1 Changed 7 years ago by Simon G. <dev@…>

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Summary changed from newforms EmailField takes the wrong maxlength when rendered as html and a CharField exists. to newforms EmailField takes the wrong max_length when rendered as html and a CharField exists.
  • Triage Stage changed from Unreviewed to Accepted

Confirmed.

Changed 7 years ago by Brian Rosner <brosner@…>

patch to fix #3578

comment:2 Changed 7 years ago by Brian Rosner <brosner@…>

  • Has patch set

comment:3 Changed 7 years ago by Brian Rosner <brosner@…>

Opps, I have a typo and the key max_length in the dict that is being returned should be maxlength .

comment:4 Changed 7 years ago by SmileyChris

  • Needs tests set
  • Patch needs improvement set

comment:5 follow-up: Changed 7 years ago by Collin Grady <cgrady@…>

Could this have been fixed already in some past changes to RegexField/URLField/EmailField ? I tried duplicating it just now and can't do it

>>> from django import newforms as forms
>>> 
>>> attrs1={'class':'aclass'}
>>> 
>>> class form1(forms.Form):
...     field1 = forms.CharField(widget=forms.TextInput(attrs=attrs1),max_length=30)
... 
>>> class form2(forms.Form):
...     field2 = forms.EmailField(widget=forms.TextInput(attrs=attrs1),max_length=10)
... 
>>> f = form2()
>>> f.as_ul()
u'<li><label for="id_field2">Field2:</label> <input id="id_field2" type="text" class="aclass" name="field2" maxlength="10" /></li>'

comment:6 in reply to: ↑ 5 Changed 7 years ago by alagu

  • Owner changed from nobody to alagu

Yeah. It WFM. Closing the ticket.

>>> from django import newforms as forms
>>> from django.newforms.widgets import *
>>> attrs1={'class':'aclass'}
>>> class form1(forms.Form):
...  field1 = forms.CharField(widget=TextInput(attrs=attrs1),max_length=30)
... 
>>> class form2(forms.Form):
...      field2 = forms.EmailField(widget=TextInput(attrs=attrs1),max_length=10)
... 
>>> f2= form2()
>>> f2.as_ul()
u'<li><label for="id_field2">Field2:</label> <input id="id_field2" type="text" class="aclass" name="field2" maxlength="10" /></li>'
>>> f2=form2({'field2':'over10@domain.com'})
>>> f2.is_valid()
False
>>> f2.errors
{}

comment:7 Changed 7 years ago by alagu

  • Resolution set to fixed
  • Status changed from new to closed

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.