Django inlineformset validation and delete

Posted by Andrew Gee on Stack Overflow See other posts from Stack Overflow or by Andrew Gee
Published on 2010-06-17T00:52:57Z Indexed on 2010/06/18 0:53 UTC
Read the original article Hit count: 322

Filed under:
|

Hi,

Can someone tell me if a form in an inlineformset should go through validation if the DELETE field is checked. I have a form that uses an inlineformset and when I check the DELETE box it fails because the required fields are blank. If I put data in the fields it will pass validation and then be deleted.

Is that how it is supposed to work, I would have thought that if it is marked for delete it would bypass the validation for that form.

Regards Andrew

Follow up - but I would still appreciate some others opinions/help
What I have figured out is that for validation to work the a formset form must either be empty or complete(valid) otherwise it will have errors when it is created and will not be deleted. As I have a couple of hidden fields in my formset forms and they are pre-populated when the page loads via javascript the form fails validation on the other required fields which might still be blank.

The way I have gotten around this by adding in a check in the add_fields that tests if the DELETE input is True and if it is it makes all fields on the form not required, which means it passes validation and will then delete.

def add_fields(self, form, index)
    #add other fields that are required....

    deleteValue = form.fields['DELETE'].widget.value_from datadict(form.data, form.files, form.add_prefix('DELETE'))
    if bool(deleteValue) or deleteValue == '':
        for name, field in form.fields.items():
            form.fields[name].required= False

This seems to be an odd way to do things but I cannot figure out another way. Is there a simpler way that I am missing?

I have also noticed that when I add the new form to my page and check the Delete box, there is no value passed back via the request, however an existing form (one loaded from the database) has a value of on when the Delete box is checked. If the box is not checked then the input is not in the request at all.

Thanks Andrew

© Stack Overflow or respective owner

Related posts about django

Related posts about inline-formset