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

Only show MultiField alert if fields have errors #980

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

mulhollandms
Copy link

I noticed that when using the 'bootstrap3' template pack, the MultiField layout object shows the alert box for errors regardless of whether there actually are any. Based on the 'layouts/multifield.html' template, it seems the only prerequisite for showing the alert is that "form_show_errors" is enabled.

This was just a quick patch to solve my problem without disabling form errors entirely so I'm sure it'll be worth another look-over before marking for review.

to indicate if multifield fields have errors
if multifield fields have errors
@codecov-io
Copy link

codecov-io commented Feb 6, 2020

Codecov Report

Merging #980 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #980      +/-   ##
==========================================
+ Coverage   96.51%   96.52%   +<.01%     
==========================================
  Files          24       24              
  Lines        2729     2732       +3     
  Branches      241      241              
==========================================
+ Hits         2634     2637       +3     
  Misses         54       54              
  Partials       41       41
Impacted Files Coverage Δ
crispy_forms/layout.py 96.29% <100%> (+0.05%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 77f4ea7...2cfb084. Read the comment docs.

@mulhollandms mulhollandms changed the title Only show MultiField if fields have errors Only show MultiField alert if fields have errors Feb 6, 2020
@smithdc1
Copy link
Member

smithdc1 commented Feb 7, 2020

hi @mulhollandms

Could I suggest a couple of other options to try fix this which don't need has_errors.

  1. Change this line to {% if form_show_errors and field.errors %}

  2. move <div class="alert alert-danger" role="alert"> within the for loop, probably after this line?

These seem to help, but I'm less familiar with bootstrap3

@mulhollandms
Copy link
Author

@smithdc1 ,

Moving the alert div inside the for loop would mean that if multiple errors are present, each field with errors will have its own separate alert with a list of errors, which looks strange and doesn't seem to line up well with the documented use:

this renders all the fields wrapped in a div and when there are errors in the form submission, they are shown in a list instead of each one surrounding the field

updated example:

image

Also, yeah I was a little hesitant about just adding has_errors to MultiField but the only other attr that indicates whether this subset of fields has errors is the css_class. On that note, I also found that with multiple errors, the MultiField div ends up with class="ctrlHolder error error", which is due to this loop.

if context['form_show_errors']:
for field in map(lambda pointer: pointer[1], self.get_field_names()):
if field in form.errors:
self.css_class += " error"

Might be worth adding a break to get out of that loop once the error class is added

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants