Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving the Pool form validation #29

Open
UjjawalKhadanga opened this issue Jan 16, 2022 · 0 comments
Open

Improving the Pool form validation #29

UjjawalKhadanga opened this issue Jan 16, 2022 · 0 comments

Comments

@UjjawalKhadanga
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant