Injecting error messages into ModelForm

61 views
Skip to first unread message

Torsten Bronger

unread,
Jul 13, 2008, 8:20:16 AM7/13/08
to django...@googlegroups.com
Hallöchen!

While ModelForm.is_valid() finds field validation errors, it cannot
catch errors in uniqueness or referential integrity as far as I can
see. Thus, I have to check them in my view.py code separately
before calling save().

If such errors are detected, I'd like to display them the same way
as the other validation errors are displayed. Is it possible to
inject additional errors into a form? Or are there better ways to
detect and report "inter-form" errors?

Tschö,
Torsten.

--
Torsten Bronger, aquisgrana, europa vetus
Jabber ID: torsten...@jabber.rwth-aachen.de

Andrew Ingram

unread,
Jul 13, 2008, 9:52:14 AM7/13/08
to django...@googlegroups.com
ModelForm is really just a convenient subclass of Form, so if you want
additional validation methods you just have to implement/override the
clean methods like you would with a normal form. The ease of validation
that newforms allows is actually one of the main reasons I prefer Django
over other frameworks i've tried.

- Andrew Ingram

Torsten Bronger

unread,
Jul 13, 2008, 10:09:15 AM7/13/08
to django...@googlegroups.com
Hallöchen!

Andrew Ingram writes:

> ModelForm is really just a convenient subclass of Form, so if you
> want additional validation methods you just have to
> implement/override the clean methods like you would with a normal
> form.

The problem is that at the time the relational integrity checks take
place, the is_valid methods have already been called. Can one call
them multiple times?

Additionally, in order to test e.g. for uniqueness, I need the
complete set of forms which I don't have in a clean() method.

Karen Tracey

unread,
Jul 13, 2008, 10:37:40 AM7/13/08
to django...@googlegroups.com
On Sun, Jul 13, 2008 at 8:20 AM, Torsten Bronger <bro...@physik.rwth-aachen.de> wrote:

Hallöchen!

While ModelForm.is_valid() finds field validation errors, it cannot
catch errors in uniqueness or referential integrity as far as I can
see.  Thus, I have to check them in my view.py code separately
before calling save().

If such errors are detected, I'd like to display them the same way
as the other validation errors are displayed.  Is it possible to
inject additional errors into a form?  Or are there better ways to
detect and report "inter-form" errors?

Ticket #7444  (http://code.djangoproject.com/ticket/7444) requests an official API to support this; in the meantime it also illustrates the method that works now -- just access the _errors dictionary directly.  I have a feeling this has come up a few times and the answer has been "we don't need an official API, just access _errors", but I could be misremembering and don't have time at the moment to research it. 

Karen

Torsten Bronger

unread,
Jul 14, 2008, 12:33:53 AM7/14/08
to django...@googlegroups.com
Hallöchen!

Karen Tracey writes:

> On Sun, Jul 13, 2008 at 8:20 AM, Torsten Bronger <
> bro...@physik.rwth-aachen.de> wrote:
>
>> While ModelForm.is_valid() finds field validation errors, it
>> cannot catch errors in uniqueness or referential integrity as far
>> as I can see. Thus, I have to check them in my view.py code
>> separately before calling save().
>>
>> If such errors are detected, I'd like to display them the same
>> way as the other validation errors are displayed. Is it possible

>> to inject additional errors into a form? [...]

> an official API to support this; [...] I have a feeling this has


> come up a few times and the answer has been "we don't need an

> official API, just access _errors", [...]

As far as I can see, an API is needed at least for Form-level
errors. For example, if a Pizza should have at least one Topping
(many-to-one relationship, I'd like to tell the user about it. But
this is certainly not associated with any field.

Karen Tracey

unread,
Jul 14, 2008, 12:43:29 AM7/14/08
to django...@googlegroups.com

This is documented, see http://www.djangoproject.com/documentation/newforms/#custom-form-and-field-validation under the discussion of the form-level clean() method:

"Note that any errors raised by your Form.clean() override will not be associated with any field in particular. They go into a special "field" (called __all__), which you can access via the non_field_errors() method if you need to."

So the 'field' for non-field-specific-errors is named '__all__'.

Karen
Reply all
Reply to author
Forward
0 new messages