Login

Must complete at least one form in formset_factory()

Author:
martino
Posted:
June 30, 2010
Language:
Python
Version:
Not specified
Score:
0 (after 0 ratings)

Workaround to stop formset_factory form being submitted completely blank. This will only allow form.is_valid() to return True if the first form has been filled in and validates.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
from django import forms

class PersonForm(forms.Form):
  first_name = forms.CharField(max_length=255)
  last_name = forms.CharField(max_length=255)
  email = forms.EmailField(max_length=255)
  
class NoEmptyFormsAllowedBaseFormSet(forms.formsets.BaseFormSet):
  """
  Won't allow formset_factory to be submitted with no forms
  """
  def clean(self):
    if "cleaned_data" in self.forms[0] and not self.forms[0].cleaned_data:
      raise forms.ValidationError, "Please fill in at least one form"



formset = formset_factory(PersonForm, extra=1, formset=NoEmptyFormsAllowedBaseFormSet)

More like this

  1. Browser-native date input field by kytta 1 month, 1 week ago
  2. Generate and render HTML Table by LLyaudet 1 month, 2 weeks ago
  3. My firs Snippets by GutemaG 1 month, 3 weeks ago
  4. FileField having auto upload_to path by junaidmgithub 2 months, 4 weeks ago
  5. LazyPrimaryKeyRelatedField by LLyaudet 3 months ago

Comments

Please login first before commenting.