formset - how to set a from error from formset.clean?

1,645 views
Skip to first unread message

Richard Brockie

unread,
Jan 17, 2015, 10:46:27 PM1/17/15
to django...@googlegroups.com
Hi everyone,

In a formset I can use the .clean() method to validate data across the formset. The formset.clean() method is run after all the form.clean() methods - this makes sense. Raising a formset ValidationError alerts the user to the problem with formset.non_form_errors.

I would like to also set an error condition on one of more of the individual forms as a result of this formset validation failure to help identify the location of the problems (my formsets can be quite long and this will help guide the eye). I've spent quite a lot of time looking for such an example with no success. Is there a way I can do this?

Here's some pseudo-code to outline what I want to do:

call MyFormSet(BaseFormSet):
   
def clean(self):
       
super(MyFormSet, self).clean()

       
# validation code goes here...

       
if some_validation_error:
           
raise ValidationError(...)    # creates the formset error message

            create_error_in_identified_forms
(somehow)    # <<<< I want to know how to do this bit

Thanks & best wishes!
R.

Collin Anderson

unread,
Jan 19, 2015, 12:16:17 PM1/19/15
to django...@googlegroups.com
Hi,

You might be able to do something like:

self.forms[3].add_error('field_name', 'error message')


Collin

Richard Brockie

unread,
Jan 19, 2015, 1:41:45 PM1/19/15
to django-users
Thank you - that looks like what I am looking for! I don't know how I missed that.

I'm on 1.6 right now - another reason to move to 1.7 :)

R.

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/Ysef5zCb7Gc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ac19922b-a623-4083-9b86-e1ab4bf5892d%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
    R.

Richard Brockie

Real-time bicycle race results - www.ontheday.net

Richard Brockie

unread,
Jan 29, 2015, 11:04:20 PM1/29/15
to django-users
Following up - this did the trick. Thanks for the pointer.
R.
Reply all
Reply to author
Forward
0 new messages