nick_rinaldi
nick_rinaldi

Reputation: 707

Why does my Django form not raise Validation Error?

I read through most SO cases regarding this issue but all are kind of specific, so I come here for help.

I have impelemented a range filter in my Django project that takes in two inputs set to a low bound and high bound value, and displays the range of data within those bounds.

What I'm trying to do, is make it so when a user inputs a higher number in the low bound than the high bound value, a ValidationError is raised for the user to see on the front end, and results are not displayed. I am a bit new to working with Django forms, but I can supply my code, and maybe someone could provide a solution

forms.py

class PlayerForm(forms.Form):

# player forms
     points_high = forms.IntegerField(validators = [MinValueValidator(0)],
                            min_value=0, 
                            label = 'Reviews',
                            required = False,
                            widget = forms.NumberInput(
                                        attrs={'id': 'pointsHigh',
                                               'name': 'pointsHigh',
                                               'href': '#',
                                               'value': '',
                                               'class': "form-control"}))

     points_low = forms.IntegerField(validators = [MinValueValidator(0)], 
                                min_value=0, 
                                required = False,
                                widget = forms.NumberInput(
                                            attrs={'id': 'pointsLow',
                                                   'name': 'pointsLow',
                                                   'href': '#',
                                                   'value': '',
                                                   'class': "form-control"}))

     def check_bounds(self):
    """custom validation to check if low bound value is higher than high bound value"""
        data = self.cleaned_data
        player_low = data['player_low']
        player_high = data['player_high']

        if player_low and player_high:
            if player_low > player_high:
                raise forms.ValidationError(_("Low bound value cannot be higher than high bound value!"))
            
        return data

views.py

def player_points(request):

    players_form = PlayerForm()

    players_high = request.GET.get('players_high')
    players_low = request.GET.get('players_low')

    stat_kwargs = {points__lte: players_high,
                   points__gte: players_low}

    players = Players.objects.filter(**stat_kwargs)

    context = {
              'players': players
              'form': players_form
        }

 return render(request, 'nba/players.html', context)

Essentially, the goal is to take user input, check it in our check_bounds function, and return the Error if it doesn't work. How can I do that?

Upvotes: 1

Views: 111

Answers (1)

Nathan Roberts
Nathan Roberts

Reputation: 838

The form will not call your function automatically. You will need to add in to your form a place to call your function. One place you could do this is you can override the form clean() function. Something like this:

def clean(self):
   cleaned_data = super().clean()
   self.check_bounds()

This will call your function when your django form would normally call the clean() function.

Also, I don't really understand the purpose of the line return data in your check_bounds() function. What is the reason you put that line in there?

Upvotes: 1

Related Questions