Ticket #5175: formsets-required_forms2-5897.diff

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

USE THIS ONE

  • django/newforms/formsets.py

     
    11from forms import Form, ValidationError
    22from fields import IntegerField, BooleanField
    33from widgets import HiddenInput
     4from util import ErrorList, ErrorDict
    45
    56# special field names
    67FORM_COUNT_FIELD_NAME = 'COUNT'
     
    2122    """A collection of instances of the same Form class."""
    2223
    2324    def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None, initial=None):
    24         self.is_bound = data is not None and files is not None
     25        self.is_bound = data is not None or files is not None
    2526        self.prefix = prefix or 'form'
    2627        self.auto_id = auto_id
    2728        self.data = data
     
    6162                    kwargs['files'] = self.files
    6263                add_form = FormClass(**kwargs)
    6364                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)
    6470                add_forms.append(add_form)
    6571            self._add_forms = add_forms
    6672        return self._add_forms
     
    6975    def _get_change_forms(self):
    7076        """Return a list of all the change forms in this ``FormSet``."""
    7177        FormClass = self.form_class
    72         if not hasattr(self, '_add_forms'):
     78        if not hasattr(self, '_change_forms'):
    7379            change_forms = []
    7480            for i in range(0, self.change_form_count):
    7581                kwargs = {'auto_id': self.auto_id, 'prefix': self.add_prefix(i)}
     
    134140        if self.orderable:
    135141            cleaned_data.sort(lambda x,y: x[ORDERING_FIELD_NAME] - y[ORDERING_FIELD_NAME])
    136142
     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        else:
     147            print cleaned_data
     148           
    137149        if is_valid:
    138150            self.cleaned_data = cleaned_data
    139151            self.deleted_data = deleted_data
     
    151163        return '%s-%s' % (self.prefix, index)
    152164
    153165    def is_valid(self):
     166        if not self.is_bound:
     167            return False
    154168        self.full_clean()
    155169        return self._is_valid
    156170
     
    161175
    162176def all_valid(formsets):
    163177    """Returns true if every formset in formsets is valid."""
     178    if_valid = True
    164179    for formset in formsets:
    165180        if not formset.is_valid():
    166             return False
    167     return True
    168  No newline at end of file
     181            is_valid = False
     182    return is_valid
     183 No newline at end of file
Back to Top