mirror of
https://github.com/django/django.git
synced 2025-11-07 07:15:35 +00:00
Fixed #27037 -- Prevented required attribute on ClearableFileInput when initial data exists.
This commit is contained in:
@@ -134,18 +134,7 @@ class BoundField(object):
|
||||
the form is not bound or the data otherwise.
|
||||
"""
|
||||
if not self.form.is_bound:
|
||||
data = self.form.initial.get(self.name, self.field.initial)
|
||||
if callable(data):
|
||||
if self._initial_value is not UNSET:
|
||||
data = self._initial_value
|
||||
else:
|
||||
data = data()
|
||||
# If this is an auto-generated default date, nix the
|
||||
# microseconds for standardized handling. See #22502.
|
||||
if (isinstance(data, (datetime.datetime, datetime.time)) and
|
||||
not self.field.widget.supports_microseconds):
|
||||
data = data.replace(microsecond=0)
|
||||
self._initial_value = data
|
||||
data = self.initial
|
||||
else:
|
||||
data = self.field.bound_data(
|
||||
self.data, self.form.initial.get(self.name, self.field.initial)
|
||||
@@ -231,11 +220,27 @@ class BoundField(object):
|
||||
id_ = widget.attrs.get('id') or self.auto_id
|
||||
return widget.id_for_label(id_)
|
||||
|
||||
@property
|
||||
def initial(self):
|
||||
data = self.form.initial.get(self.name, self.field.initial)
|
||||
if callable(data):
|
||||
if self._initial_value is not UNSET:
|
||||
data = self._initial_value
|
||||
else:
|
||||
data = data()
|
||||
# If this is an auto-generated default date, nix the
|
||||
# microseconds for standardized handling. See #22502.
|
||||
if (isinstance(data, (datetime.datetime, datetime.time)) and
|
||||
not self.field.widget.supports_microseconds):
|
||||
data = data.replace(microsecond=0)
|
||||
self._initial_value = data
|
||||
return data
|
||||
|
||||
def build_widget_attrs(self, attrs, widget=None):
|
||||
if not widget:
|
||||
widget = self.field.widget
|
||||
attrs = dict(attrs) # Copy attrs to avoid modifying the argument.
|
||||
if not widget.is_hidden and self.field.required and self.form.use_required_attribute:
|
||||
if widget.use_required_attribute(self.initial) and self.field.required and self.form.use_required_attribute:
|
||||
attrs['required'] = True
|
||||
if self.field.disabled:
|
||||
attrs['disabled'] = True
|
||||
|
||||
@@ -248,6 +248,9 @@ class Widget(six.with_metaclass(RenameWidgetMethods)):
|
||||
"""
|
||||
return id_
|
||||
|
||||
def use_required_attribute(self, initial):
|
||||
return not self.is_hidden
|
||||
|
||||
|
||||
class Input(Widget):
|
||||
"""
|
||||
@@ -429,6 +432,9 @@ class ClearableFileInput(FileInput):
|
||||
return False
|
||||
return upload
|
||||
|
||||
def use_required_attribute(self, initial):
|
||||
return super(ClearableFileInput, self).use_required_attribute(initial) and not initial
|
||||
|
||||
|
||||
class Textarea(Widget):
|
||||
def __init__(self, attrs=None):
|
||||
@@ -795,12 +801,10 @@ class CheckboxSelectMultiple(RendererMixin, SelectMultiple):
|
||||
renderer = CheckboxFieldRenderer
|
||||
_empty_value = []
|
||||
|
||||
def build_attrs(self, extra_attrs=None, **kwargs):
|
||||
attrs = super(CheckboxSelectMultiple, self).build_attrs(extra_attrs, **kwargs)
|
||||
# Remove the 'required' attribute because browser validation would
|
||||
def use_required_attribute(self, initial):
|
||||
# Don't use the 'required' attribute because browser validation would
|
||||
# require all checkboxes to be checked instead of at least one.
|
||||
attrs.pop('required', None)
|
||||
return attrs
|
||||
return False
|
||||
|
||||
|
||||
class MultiWidget(Widget):
|
||||
|
||||
Reference in New Issue
Block a user