Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refs #938 -- Avoid attributes being duplicated #1194

Merged
merged 2 commits into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
@@ -0,0 +1,38 @@
<form method="post">
<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" 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" 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" 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" 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" required id="id_password2" /></div>
</div>
</div>
</form>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<form method="post">
<div id="div_id_email" class="form-group has-error">
<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" required id="id_email" /> <span class="input-group-addon">whatever</span></div>
<span id="error_1_id_email" class="help-block"><strong>Enter a valid email address.</strong></span>
<div id="hint_id_email" class="help-block">Insert your email</div>
</div>
</div>
<div id="div_id_first_name" class="form-group has-error">
<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" required id="id_first_name" />
</div>

<span id="error_1_id_first_name" class="help-block"><strong>Ensure this value has at most 5 characters (it has 19).</strong></span>
</div>
</div>
<div id="div_id_last_name" class="form-group has-error">
<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" required id="id_last_name" />
<span class="input-group-addon">bar</span>
</div>
<span id="error_1_id_last_name" class="help-block"><strong>Ensure this value has at most 5 characters (it has 18).</strong></span>
</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" 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" required id="id_password2" /></div>
</div>
</div>
</form>
6 changes: 2 additions & 4 deletions crispy_forms/tests/test_form_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,12 +637,10 @@ def test_bootstrap_form_show_errors_bs3():
form.is_valid()

form.helper.form_show_errors = True
html = render_crispy_form(form)
assert html.count("error") == 6
assert parse_form(form) == parse_expected("bootstrap3/test_form_helper/bootstrap_form_show_errors_bs3_true.html")

form.helper.form_show_errors = False
html = render_crispy_form(form)
assert html.count("error") == 0
assert parse_form(form) == parse_expected("bootstrap3/test_form_helper/bootstrap_form_show_errors_bs3_false.html")


@only_bootstrap4
Expand Down