def test_formset_errors(self):
        """Formset errors get propagated properly."""
        data = {
            'formset-emails-INITIAL_FORMS': 0,
            'formset-emails-TOTAL_FORMS': 1,
            'formset-emails-MAX_NUM_FORMS': 3,
            'formset-emails-0-email': 'foobar',
            'username': '******',
            'form-nested_form-name': 'Some Name',
        }
        form = AccountForm(data)

        if django.VERSION >= (1, 5):
            expected_errors = ErrorList([
                ErrorDict({
                    u'email': ErrorList([u'Enter a valid email address.'])
                })
            ])
        else:
            # Fix for Django 1.4
            expected_errors = ErrorList([
                ErrorDict({
                    u'email': ErrorList([u'Enter a valid e-mail address.'])
                })
            ])

        self.assertFalse(form.is_valid())
        self.assertEqual(form.errors['emails'], expected_errors)
 def full_clean(self):
     """
     Clean the form, including all formsets and add formset errors to the
     errors dict. Errors of nested forms and formsets are only included if
     they actually contain errors.
     """
     super(SuperFormMixin, self).full_clean()
     for field_name, composite in self.forms.items():
         composite.full_clean()
         if not composite.is_valid() and composite._errors:
             self._errors[field_name] = ErrorDict(composite._errors)
     for field_name, composite in self.formsets.items():
         composite.full_clean()
         if not composite.is_valid() and composite._errors:
             self._errors[field_name] = ErrorList(composite._errors)
示例#3
0
 def form_valid(self, form):
     with NamedTemporaryFile(suffix=".csv") as fh:
         importer = CsvImporter(fh.name)
         reader = csv.DictReader(form.cleaned_data["csv_file"])
         try:
             importer.import_csv(reader)
         except MissingRequiredColumn, e:
             error_msg = "File is missing required column '{}'".format(e)
             # TODO: Don't do this dirty hack
             form._errors["csv_file"] = ErrorList([error_msg])
             return self.form_invalid(form)
         messages.info(
             self.request,
             "Imported {} addresses".format(importer.import_count))
         if importer.reject_count > 0:
             fh.seek(0)
             context = self.get_context_data(form)
             context.update({
                 "reject_count":
                 importer.reject_count,
                 "rejected_emails":
                 fh.read().strip().split("\n"),
             })
             return self.render_to_response(context)