Skip to content

Commit

Permalink
Refs #938 -- Avoid attributes being duplicated
Browse files Browse the repository at this point in the history
Prior to this patch attrs is added to the widget each time the form is rendered. This could lead to multiple `form-control` css classes being rendered.
  • Loading branch information
smithdc1 committed Nov 1, 2021
1 parent 5b41e50 commit 7d1b1fa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
19 changes: 5 additions & 14 deletions crispy_forms/templatetags/crispy_forms_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ class CrispyFieldNode(template.Node):
def __init__(self, field, attrs):
self.field = field
self.attrs = attrs
self.html5_required = "html5_required"

def render(self, context): # noqa: C901
# Nodes are not threadsafe so we must store and look up our instance
Expand All @@ -84,15 +83,10 @@ def render(self, context): # noqa: C901
context.render_context[self] = (
template.Variable(self.field),
self.attrs,
template.Variable(self.html5_required),
)

field, attrs, html5_required = context.render_context[self]
field, attrs = context.render_context[self]
field = field.resolve(context)
try:
html5_required = html5_required.resolve(context)
except template.VariableDoesNotExist:
html5_required = False

# If template pack has been overridden in FormHelper we can pick it from context
template_pack = context.get("template_pack", TEMPLATE_PACK)
Expand Down Expand Up @@ -131,18 +125,15 @@ def render(self, context): # noqa: C901

widget.attrs["class"] = css_class

# HTML5 required attribute
if html5_required and field.field.required and "required" not in widget.attrs:
if field.field.widget.__class__.__name__ != "RadioSelect":
widget.attrs["required"] = "required"

for attribute_name, attribute in attr.items():
attribute_name = template.Variable(attribute_name).resolve(context)
attribute = template.Variable(attribute).resolve(context)

if attribute_name in widget.attrs:
widget.attrs[attribute_name] += " " + template.Variable(attribute).resolve(context)
if attribute not in widget.attrs[attribute_name]:
widget.attrs[attribute_name] += " " + attribute
else:
widget.attrs[attribute_name] = template.Variable(attribute).resolve(context)
widget.attrs[attribute_name] = attribute

return str(field)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,37 @@
<div id="div_id_email" class="form-group">
<label for="id_email" class=" control-label requiredField"> email<span class="asteriskField">*</span> </label>
<div class=" controls">
<div class="input-group"><input type="text" name="email" value="invalidemail" maxlength="30" class="textinput textInput inputtext form-control form-control" required id="id_email" /> <span class="input-group-addon">whatever</span></div>
<div class="input-group"><input type="text" name="email" value="invalidemail" maxlength="30" class="textinput textInput inputtext form-control" required id="id_email" /> <span class="input-group-addon">whatever</span></div>
<div id="hint_id_email" class="help-block">Insert your email</div>
</div>
</div>
<div id="div_id_first_name" class="form-group">
<label for="id_first_name" class=" control-label requiredField"> first name<span class="asteriskField">*</span> </label>
<div class=" controls">
<div class="input-group">
<span class="input-group-addon">blabla</span> <input type="text" name="first_name" value="first_name_too_long" maxlength="5" class="textinput textInput inputtext form-control form-control" required id="id_first_name" />
<span class="input-group-addon">blabla</span> <input type="text" name="first_name" value="first_name_too_long" maxlength="5" class="textinput textInput inputtext form-control" required id="id_first_name" />
</div>
</div>
</div>
<div id="div_id_last_name" class="form-group">
<label for="id_last_name" class=" control-label requiredField"> last name<span class="asteriskField">*</span> </label>
<div class=" controls">
<div class="input-group">
<span class="input-group-addon">foo</span> <input type="text" name="last_name" value="last_name_too_long" maxlength="5" class="textinput textInput inputtext form-control form-control" required id="id_last_name" />
<span class="input-group-addon">foo</span> <input type="text" name="last_name" value="last_name_too_long" maxlength="5" class="textinput textInput inputtext form-control" required id="id_last_name" />
<span class="input-group-addon">bar</span>
</div>
</div>
</div>
<div id="div_id_password1" class="form-group">
<label for="id_password1" class=" control-label requiredField"> password<span class="asteriskField">*</span> </label>
<div class=" controls">
<div class="input-group"><input type="password" name="password1" maxlength="30" class="textinput textInput form-control form-control" required id="id_password1" /> <span class="input-group-addon">whatever</span></div>
<div class="input-group"><input type="password" name="password1" maxlength="30" class="textinput textInput form-control" required id="id_password1" /> <span class="input-group-addon">whatever</span></div>
</div>
</div>
<div id="div_id_password2" class="form-group">
<label for="id_password2" class=" control-label requiredField"> re-enter password<span class="asteriskField">*</span> </label>
<div class=" controls">
<div class="input-group"><span class="input-group-addon">blabla</span> <input type="password" name="password2" maxlength="30" class="textinput textInput form-control form-control" required id="id_password2" /></div>
<div class="input-group"><span class="input-group-addon">blabla</span> <input type="password" name="password2" maxlength="30" class="textinput textInput form-control" required id="id_password2" /></div>
</div>
</div>
</form>

0 comments on commit 7d1b1fa

Please sign in to comment.