You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When trying to do {{ form.field | remove_attr:'required' }} as proposed in #63, the resulting <input> still has the "required" attribute.
If I have understood the code correctly, at least in the case of django stock authentication forms, this happens because the "required" attribute is not initially included in field.field.widget.attrs, but rather is set by the BoundField class in the build_widget_attrs() method at a later stage after the filter has run. Probably the same happens with the "disabled" attribute.
Code references:
@register.filter("remove_attr")
@silence_without_field
def remove_attr(field, attr):
if attr in field.field.widget.attrs:
del field.field.widget.attrs[attr]
return field
def build_widget_attrs(self, attrs, widget=None):
widget = widget or self.field.widget
attrs = dict(attrs) # Copy attrs to avoid modifying the argument.
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
return attrs
django/django/forms/boundfield.py
A possible solution would be:
@register.filter("remove_attr")
@silence_without_field
def remove_attr(field, attr):
if attr == "required":
field.field.required = False
else:
if attr in field.field.widget.attrs:
del field.field.widget.attrs[attr]
return field
The text was updated successfully, but these errors were encountered:
I guess the use case here is the HTML5 validation that is triggered with required being set on the field.
The one possible work-around I found is setting novalidate on the <form> tag (<form novalidate action...>).
If the rendered field have the required attribute, not even formnovalidate on it is going to work.
When trying to do
{{ form.field | remove_attr:'required' }}
as proposed in #63, the resulting<input>
still has the "required" attribute.If I have understood the code correctly, at least in the case of django stock authentication forms, this happens because the "required" attribute is not initially included in
field.field.widget.attrs
, but rather is set by the BoundField class in thebuild_widget_attrs()
method at a later stage after the filter has run. Probably the same happens with the "disabled" attribute.Code references:
django-widget-tweaks/widget_tweaks/templatetags/widget_tweaks.py
django/django/forms/boundfield.py
A possible solution would be:
The text was updated successfully, but these errors were encountered: