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
I am using Floppyforms in an elaborate form page consisting of several nested ModelForm objects, and some form sets. Some of these form sets have min_num=0, which causes the initial forms to be created with empty_permitted = True. That is, the server-side validation won't complain if the entire form is empty. Required fields are only enforced if the form has been modified. However, even for those forms, any fields marked as required are given the HTML5 required attribute, making it impossible to submit the form without filling them in.
Steps to reproduce:
Create a Form with a required field.
Instantiate the form with empty_permitted=True.
When the form is rendered, it should be possible to submit it in a modern browser without setting a value for the field. Instead, the client-side validation rules make this impossible: the browser will complain that the field requires a value.
And a simple code example:
import floppyforms as forms
class OptionalForm(forms.Form):
name = forms.CharField(required=True)
optional_form = OptionalForm(empty_permitted=True)
print(optional_form.as_p().strip())
Here's a workaround I have been using. A complete solution also needs some JavaScript code that interprets data-was-required and does fun things:
class TKDModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(TKDModelForm, self).__init__(*args, **kwargs)
if self.empty_permitted:
for key, field in self.fields.iteritems():
if field.widget.is_required:
field.widget.is_required = False
field.widget.attrs.pop('required', None)
field.widget.attrs['data-was-required'] = True
This goes through every field in the form, and if the widget has been marked as required we make sure to strip the required attribute, set is_required = False (so it isn't passed to the widget's template), and then add our own attribute to keep track of this field's more complex validation rules.
The text was updated successfully, but these errors were encountered:
Hm, I think I see the problem here. We have a server-side behaviour in the form (allow either all fields to be empty, unless at least one field is filled in, then require all to be filled in) that we cannot replicate with a html-only solution.
But I know that this does not replicate the behaviour you expect. What would you suggest as a solution? Remove the <input required> attributes when empty_permitted=True?
I am using Floppyforms in an elaborate form page consisting of several nested ModelForm objects, and some form sets. Some of these form sets have min_num=0, which causes the initial forms to be created with
empty_permitted = True
. That is, the server-side validation won't complain if the entire form is empty. Required fields are only enforced if the form has been modified. However, even for those forms, any fields marked as required are given the HTML5 required attribute, making it impossible to submit the form without filling them in.Steps to reproduce:
And a simple code example:
Expected output:
Actual output:
Workaround
Here's a workaround I have been using. A complete solution also needs some JavaScript code that interprets data-was-required and does fun things:
This goes through every field in the form, and if the widget has been marked as required we make sure to strip the required attribute, set
is_required = False
(so it isn't passed to the widget's template), and then add our own attribute to keep track of this field's more complex validation rules.The text was updated successfully, but these errors were encountered: