Code

Ticket #5175: formsets-required_forms2-5897.diff

File formsets-required_forms2-5897.diff, 2.8 KB (added by Simon Litchfield <simon@…>, 7 years ago)

USE THIS ONE

Line 
1Index: django/newforms/formsets.py
2===================================================================
3--- django/newforms/formsets.py (revision 5897)
4+++ django/newforms/formsets.py (working copy)
5@@ -1,6 +1,7 @@
6 from forms import Form, ValidationError
7 from fields import IntegerField, BooleanField
8 from widgets import HiddenInput
9+from util import ErrorList, ErrorDict
10 
11 # special field names
12 FORM_COUNT_FIELD_NAME = 'COUNT'
13@@ -21,7 +22,7 @@
14     """A collection of instances of the same Form class."""
15 
16     def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None, initial=None):
17-        self.is_bound = data is not None and files is not None
18+        self.is_bound = data is not None or files is not None
19         self.prefix = prefix or 'form'
20         self.auto_id = auto_id
21         self.data = data
22@@ -61,6 +62,11 @@
23                     kwargs['files'] = self.files
24                 add_form = FormClass(**kwargs)
25                 self.add_fields(add_form, i)
26+                # HACK: Form is empty, lets re-create without data so it doesn't trip validation
27+                if self.data and add_form.is_empty():
28+                    del(kwargs['data'])
29+                    add_form = FormClass(**kwargs)
30+                    self.add_fields(add_form, i)
31                 add_forms.append(add_form)
32             self._add_forms = add_forms
33         return self._add_forms
34@@ -69,7 +75,7 @@
35     def _get_change_forms(self):
36         """Return a list of all the change forms in this ``FormSet``."""
37         FormClass = self.form_class
38-        if not hasattr(self, '_add_forms'):
39+        if not hasattr(self, '_change_forms'):
40             change_forms = []
41             for i in range(0, self.change_form_count):
42                 kwargs = {'auto_id': self.auto_id, 'prefix': self.add_prefix(i)}
43@@ -134,6 +140,12 @@
44         if self.orderable:
45             cleaned_data.sort(lambda x,y: x[ORDERING_FIELD_NAME] - y[ORDERING_FIELD_NAME])
46 
47+        if is_valid and self.required_forms > len(cleaned_data):
48+            self.formset_errors = ErrorList(['At least %s data set(s) are required.' % self.required_forms])
49+            is_valid = False
50+        else:
51+            print cleaned_data
52+           
53         if is_valid:
54             self.cleaned_data = cleaned_data
55             self.deleted_data = deleted_data
56@@ -151,6 +163,8 @@
57         return '%s-%s' % (self.prefix, index)
58 
59     def is_valid(self):
60+        if not self.is_bound:
61+            return False
62         self.full_clean()
63         return self._is_valid
64 
65@@ -161,7 +175,8 @@
66 
67 def all_valid(formsets):
68     """Returns true if every formset in formsets is valid."""
69+    if_valid = True
70     for formset in formsets:
71         if not formset.is_valid():
72-            return False
73-    return True
74\ No newline at end of file
75+            is_valid = False
76+    return is_valid
77\ No newline at end of file