views:

32

answers:

1

Hi,

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?

+1  A: 

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

Anurag Uniyal
Yeah, I've thought about that and it should work, but doesn't seem very clean. Then I have to decide before I know the results which form that should raise the ValidationError. And the form gets coupled to the other.
Espen Christensen
but when forms are already related you can not avoid coupling, you are just trying to move validation code from form to outside form which IMO is more unclean
Anurag Uniyal
They are not necessarily related, but in this one use case they are, but the forms can be used unrelated other places.
Espen Christensen