Ticket #5175: required_forms-formsets.diff
File required_forms-formsets.diff, 2.8 KB (added by , 17 years ago) |
---|
-
django/newforms/formsets.py
1 1 from forms import Form, ValidationError 2 2 from fields import IntegerField, BooleanField 3 3 from widgets import HiddenInput 4 from util import ErrorList, ErrorDict 4 5 5 6 # special field names 6 7 FORM_COUNT_FIELD_NAME = 'COUNT' … … 21 22 """A collection of instances of the same Form class.""" 22 23 23 24 def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None, initial=None): 24 self.is_bound = data is not None andfiles is not None25 self.is_bound = data is not None or files is not None 25 26 self.prefix = prefix or 'form' 26 27 self.auto_id = auto_id 27 28 self.data = data … … 61 62 kwargs['files'] = self.files 62 63 add_form = FormClass(**kwargs) 63 64 self.add_fields(add_form, i) 65 # HACK: Form is empty, lets re-create without data so it doesn't trip validation 66 if self.data and add_form.is_empty(): 67 del(kwargs['data']) 68 add_form = FormClass(**kwargs) 69 self.add_fields(add_form, i) 64 70 add_forms.append(add_form) 65 71 self._add_forms = add_forms 66 72 return self._add_forms … … 69 75 def _get_change_forms(self): 70 76 """Return a list of all the change forms in this ``FormSet``.""" 71 77 FormClass = self.form_class 72 if not hasattr(self, '_ add_forms'):78 if not hasattr(self, '_change_forms'): 73 79 change_forms = [] 74 80 for i in range(0, self.change_form_count): 75 81 kwargs = {'auto_id': self.auto_id, 'prefix': self.add_prefix(i)} … … 134 140 if self.orderable: 135 141 cleaned_data.sort(lambda x,y: x[ORDERING_FIELD_NAME] - y[ORDERING_FIELD_NAME]) 136 142 143 if is_valid and self.required_forms > len(cleaned_data): 144 self.formset_errors = ErrorList(['At least %s data set(s) are required.' % self.required_forms]) 145 is_valid = False 146 137 147 if is_valid: 138 148 self.cleaned_data = cleaned_data 139 149 self.deleted_data = deleted_data … … 151 161 return '%s-%s' % (self.prefix, index) 152 162 153 163 def is_valid(self): 164 if not self.is_bound: 165 return False 154 166 self.full_clean() 155 167 return self._is_valid 156 168 … … 161 173 162 174 def all_valid(formsets): 163 175 """Returns true if every formset in formsets is valid.""" 176 if_valid = True 164 177 for formset in formsets: 165 178 if not formset.is_valid(): 166 returnFalse167 return True168 No newline at end of file 179 is_valid = False 180 return is_valid 181 No newline at end of file