espenhogbakk
espenhogbakk

Reputation: 11728

Django form validation in views

I was wondering if it was possible to do form validation in django views. I have usecases where I have one view with 2 forms. Each forms has their own clean method and such, but what i can't do at the form level is to check if one value is present in the one form, do something else in the other form.

This is simple to check in a view, but I cant find a way to update the non_field_errors() list and raise a ValidationError in a view. I can of course raise a ValidationError in a view, but that doesn't return back to the form and display the error.

Is this possible?

Upvotes: 3

Views: 1896

Answers (2)

henrikstroem
henrikstroem

Reputation: 3068

Bit of a late answer, but you can use

form.add_error(<field>, <errormsg>)

to invalidate a form field from a view function.

Upvotes: 0

Anurag Uniyal
Anurag Uniyal

Reputation: 88777

You can Pass the first form to second form's __init__ and check in clean or clean_xxx methods whatever you wish to check.

Upvotes: 1

Related Questions