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

Removing required='' for FileFields that have a value set #38

Merged
merged 3 commits into from Oct 6, 2018
Merged
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
9 changes: 7 additions & 2 deletions crispy_forms_foundation/forms.py
Expand Up @@ -4,6 +4,7 @@

from django import forms
from django.core.urlresolvers import reverse, NoReverseMatch
from django.forms.fields import FileField, ImageField
from django.utils.translation import ugettext_lazy as _

from crispy_forms.helper import FormHelper
Expand Down Expand Up @@ -65,8 +66,12 @@ def init_helper(self):
# Put required HTML attribute on required fields so they are managed by
# Abide (if enabled)
if "data_abide" in self.attrs:
for field in self.fields.values():
if field.required:
for field_name, field in self.fields.items():
if hasattr(self, 'instance'):
field_value = getattr(self.instance, field_name, None)
else:
field_value = None
if field.required and not ((isinstance(field, FileField) or isinstance(field, ImageField)) and field_value):
field.widget.attrs["required"] = ""
field.abide_msg = _("This field is required.")

Expand Down