Skip to content

Improving the Pool form validation #29

@UjjawalKhadanga

Description

@UjjawalKhadanga

Problem

Currently, these are the problems with validation of pool form:

  • Seats can be assigned zero and negative values.
  • User can select any past datetime values.
  • Source and Destination can be same.
  • Unpaid trips store the amount in database(if the paid box is unchecked, then the amount stored in database should be 0)
  • Amount on paid trips cannot be negative

Solution

We can override the default form.clean() method on class PoolForm() and make a custom clean method.

    def clean(self):
        cleaned_data = super(PoolForm,self).clean()
        
        if cleaned_data.get('tot') <= 0:
            raise forms.ValidationError("There should be atleast 1 seats")
            
        if datetime.strptime(cleaned_data.get('dateTime'),"%Y-%m-%d %H:%M") <= datetime.now():
            raise forms.ValidationError("Selected DateTime has passed. Please select a valid date.")

        if cleaned_data.get('source') == cleaned_data.get('dest'):
            raise forms.ValidationError("Source and Destination should be different") 
        
        if cleaned_data.get('paid') == False:
            self.cleaned_data['amount']=0
        else :
            if cleaned_data.get('amount') < 0:
                raise forms.ValidationError("Amount should be positive")

        
        return cleaned_data

@dipanshu231099 Please review this issue and its solution

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions