Code

Opened 22 months ago

Closed 17 months ago

Last modified 17 months ago

#18574 closed Bug (fixed)

`BaseFormSet.is_valid` should call it's underlying forms' `is_valid`

Reported by: charettes Owned by: nobody
Component: Forms Version: 1.4
Severity: Normal Keywords: form formset is_valid
Cc: foonicorn Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The actual implementation of BaseFormSet.is_valid checks if any of it's underlying forms have errors which short circuits any extra check-ups that could be defined in the form is_valid method.

i,e.

class MyForm(Form):

  def is_valid(self):
    is_valid = super(MyForm, self).is_valid()
    print 'I was called'
    return is_valid and self.other_non_field_related_validation()

MyFormSet = formset_factory(MyForm)

# In this case calling MyFormSet(data).is_valid() will never print 'I was called'.

I'm submitting this as a bug since I don't think that's the expected behaviour.

Attachments (1)

18574_2.diff (2.3 KB) - added by foonicorn 17 months ago.
Tests for charettes' patch

Download all attachments as: .zip

Change History (9)

comment:1 Changed 22 months ago by charettes

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Created a pull request which passes all tests on sqlite.

comment:2 Changed 22 months ago by charettes

  • Has patch set

comment:3 Changed 22 months ago by claudep

  • Needs tests set
  • Triage Stage changed from Unreviewed to Accepted

comment:4 Changed 22 months ago by charettes

Here's the workaround I use while this is not fixed:

from django.forms.formsets import BaseFormSet


class BaseValidateFormSet(BaseFormSet):
    
    def is_valid(self):
        is_valid = super(BaseValidateFormSet, self).is_valid()
        for form in self:
            is_valid &= form.is_valid() or (self.can_delete and
                                            self._should_delete_form(form))
        return is_valid

Changed 17 months ago by foonicorn

Tests for charettes' patch

comment:5 Changed 17 months ago by foonicorn

  • Cc foonicorn added
  • Needs tests unset

comment:6 Changed 17 months ago by claudep

  • Triage Stage changed from Accepted to Ready for checkin

comment:7 Changed 17 months ago by Claude Paroz <claude@…>

  • Resolution set to fixed
  • Status changed from new to closed

In 66dfcc10b37120638894cb412a15274b17cfb360:

Fixed #18574 -- Make BaseFormSet.is_valid call its underlying forms' is_valid

Thanks Simon Charette for the report and the initial patch.

comment:8 Changed 17 months ago by Claude Paroz <claude@…>

In 67bddc0b7b6468abe875f30e4b70560787b5eb5b:

[1.5.x] Fixed #18574 -- Make BaseFormSet.is_valid call its underlying forms' is_valid

Thanks Simon Charette for the report and the initial patch.
Backport of 66dfcc10b from master.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.