diff options
| author | Jon Dufresne <jon.dufresne@gmail.com> | 2016-07-21 17:16:22 -0700 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-08-01 10:36:17 -0400 |
| commit | ac3aaaa740dcf9c6efd2f88ee9219c1924c7695e (patch) | |
| tree | 72e8a094b6f88f60e80397eaf00ce52feb46ed6f /django | |
| parent | aad46c3e370e105f9117a337924090d05f1b001d (diff) | |
Fixed #26927 -- Made subwidget iteration pass disabled and required attributes.
Diffstat (limited to 'django')
| -rw-r--r-- | django/forms/boundfield.py | 16 | ||||
| -rw-r--r-- | django/forms/widgets.py | 7 |
2 files changed, 19 insertions, 4 deletions
diff --git a/django/forms/boundfield.py b/django/forms/boundfield.py index 4867e72deb..df3de5848e 100644 --- a/django/forms/boundfield.py +++ b/django/forms/boundfield.py @@ -51,6 +51,7 @@ class BoundField(object): """ id_ = self.field.widget.attrs.get('id') or self.auto_id attrs = {'id': id_} if id_ else {} + attrs = self.build_widget_attrs(attrs) for subwidget in self.field.widget.subwidgets(self.html_name, self.value(), attrs): yield subwidget @@ -85,10 +86,7 @@ class BoundField(object): widget.is_localized = True attrs = attrs or {} - if not widget.is_hidden and self.field.required and self.form.use_required_attribute: - attrs['required'] = True - if self.field.disabled: - attrs['disabled'] = True + attrs = self.build_widget_attrs(attrs, widget) auto_id = self.auto_id if auto_id and 'id' not in attrs and 'id' not in widget.attrs: if not only_initial: @@ -227,3 +225,13 @@ class BoundField(object): widget = self.field.widget id_ = widget.attrs.get('id') or self.auto_id return widget.id_for_label(id_) + + 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: + attrs['required'] = True + if self.field.disabled: + attrs['disabled'] = True + return attrs diff --git a/django/forms/widgets.py b/django/forms/widgets.py index 604a6a401a..217f37b022 100644 --- a/django/forms/widgets.py +++ b/django/forms/widgets.py @@ -787,6 +787,13 @@ 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 + # require all checkboxes to be checked instead of at least one. + attrs.pop('required', None) + return attrs + class MultiWidget(Widget): """ |
