Melly
Melly

Reputation: 745

forms.ValidationError bug?

this is my third day in django and I'm working on my validation form and i came across this weird bug where my validation form didn't do anything so here's the code

class RegisterForm(forms.ModelForm):

email = forms.EmailField(label="E-Mail", error_messages={'required': 'Please enter your name'})

class Meta:
    model = LowUser
    fields =['fullname', 'email', 'password', 'bio']
    widgets = {
        'password' : forms.PasswordInput()
        
    }

def clean_fullname(self):
    data = self.cleaned_data.get("fullname")

    if 'ab' in data:
         raise forms.ValidationError('invalid')
    else:
        return data

if i input "ac" to the fullname it works perfectly fine it adds the input to the database. But if i input "ab" it didn't do anything it doesn't give me any errors nor add the input to my database. And I'm pretty sure my forms.ValidationError is bugging because if i change my raise forms.ValidationError('invalid') to raise NameError('Test') like this

def clean_fullname(self):
    data = self.cleaned_data.get("fullname")

    if 'ab' in data:
         raise NameError('Test')

    else:
        return data

and i input "ab". It works completely fine and it gave me this page

enter image description here

and I'm using django 2.1.5 if you're wondering i would appreciate any help

thank you in advance

Upvotes: 1

Views: 65

Answers (2)

Melly
Melly

Reputation: 745

Thanks to Willem i realized that the problem was in my views.py.

def registerForm(request):


  regisform = RegisterForm()
  cntxt = {'mainregisform': regisform, 'tst': ''} 
  if request.method == 'POST':
      regisform = RegisterForm(request.POST)

      if regisform.is_valid():
          regisform.save()



   return render(request, 'userregister.html', cntxt)

i thought that the ValidationError wasn't giving me any errors because usually there's an error message on top of my input box, but it actually did gave me an error. the problem was i define the mainregisform before the regisform got re-rendered therefore i never got error message

Upvotes: 1

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476614

If i input "ac" to the fullname it works perfectly fine it adds the input to the database. But if i input "ab" it didn't do anything it doesn't give me any errors nor add the input to my database.

That is expected behavior, ValidationErrors are used to collect all errors.

The idea is that you raise ValidationErrors. These are all collected, and when one such error is present, form.is_valid() will return False, and the form.errors will contain a dictionary-like object with all the errors. The reason this is done is to collect problems with all fields in one single pass, such that it does not only report the first error of the form data.

Imagine that you have five fields with mistakes, and the form only reports problems with the first field. Then it takes five rounds before all fields are validated. By collecting all errors, it can show multiple ones. You can even return multiple errors on the same field.

For more information, see the raising ValidationError` section of the Django documentation.

Upvotes: 1

Related Questions