forms.py 721 B

1234567891011121314151617181920212223242526
  1. from django import forms
  2. from django.core.validators import RegexValidator
  3. from . models import User
  4. numeric = RegexValidator(r'^[0-9+]', 'Only numeric characters.')
  5. class LoginForm(forms.Form):
  6. pin = forms.CharField(strip=True, widget=forms.PasswordInput(attrs={
  7. 'class': 'form-control form-control-lg p-4 text-center',
  8. 'id': 'pin',
  9. }), label=None, validators=[numeric])
  10. class UserForm(forms.Form):
  11. def clean(self):
  12. cleaned_data = self.cleaned_data
  13. pin = cleaned_data['pin']
  14. if pin and User.objects.get(pin=pin):
  15. raise forms.ValidationError("not unique")
  16. # Always return the full collection of cleaned data.
  17. return cleaned_data