raise form validation after form.is_valid django

0 votes
asked Apr 5 by anonymous

In my django app i have a form which is doing condition testing after getting form data, here is an example of what i am doing.

form = SomeForm(data=request.POST or None)
    if request.method == 'POST':
        if form.is_valid():
            data = form.cleaned_data
            xyz = data['xyz']

i have to perform some validation rules for xyz, what i am trying to achieve is this

if (xyz = somecodition)
    raise error..

1 Answer

0 votes
answered Apr 5 by anonymous

This not a good way to handle from validation after form is validated, there is already clean and clean_field method present for forms, i recommend to use them instead.

in your SomeForm add clean_field or clean method.


class SomeForm(forms.Form):
    xyz = ...
    def clean_xyz(self):
        data = self.cleaned_data['xyz']
        if (condition):
            raise forms.ValidationError("Your form validation message here")
        return data
commented Apr 5 by anonymous
In my case 'xyz' is not part of the form
Welcome , qPocho is an online community for programmers share there knowledge.