Beebhack
Advertisement
from django import forms
from django.contrib.auth import get_user_model
User=get_user_model()


class ContactForm(forms.Form):
fullname = forms.CharField(
widget=forms.TimeInput(attrs={"class": "form-control", "placeholder": 'Enter Your Name'}))
email = forms.EmailField(
widget=forms.EmailInput(attrs={"class": "form-control", "placeholder": "please enter your email"}))
textarea = forms.CharField(
widget=forms.Textarea(attrs={"class": "form-control", "placeholder": "please enter your text"}))

def clean_email(self):
email = self.cleaned_data.get("email")
if "@gmail.com" not in email:
raise forms.ValidationError("error not gmail")
return email


class LoginForm(forms.Form):
username = forms.CharField(widget=forms.TextInput(attrs={"class": "form-control"}))
password = forms.CharField(widget=forms.PasswordInput(attrs={"class": "form-control"}))


class RegiterForm(forms.Form):
fullname = forms.CharField(label="FullName",widget=forms.TextInput(attrs={"class":"form-control"}))
age=forms.CharField(label="Age",widget=forms.TextInput(attrs={"class":"form-control"}))
username = forms.CharField(label="UserName",widget=forms.TextInput(attrs={"class": "form-control"}))
password = forms.CharField(label="Password",widget=forms.PasswordInput(attrs={"class": "form-control"}))
confirm_password = forms.CharField(label="Confirm Password",widget=forms.PasswordInput(attrs={"class": "form-control"}))
email = forms.EmailField(label="Email",widget=forms.EmailInput(attrs={"class": "form-control"}))

def clean_username(self):
username=self.cleaned_data.get("username")
qs=User.objects.filter(username=username)
if qs.exists():
raise forms.ValidationError("username is taken")
return username
def clean_email(self):
email = self.cleaned_data.get("email")
qs = User.objects.filter(email=email)
if qs.exists():
raise forms.ValidationError("email is taken")
return email

def clean_password(self):
data=self.cleaned_data
password=self.cleaned_data.get("password")
confirm_pass=self.cleaned_data.get("confirm_password")
if len(password)<7:
raise forms.ValidationError("pass lost")
elif password !=confirm_pass:
raise forms.ValidationError("pass lost")
if password == confirm_pass:
return data
Advertisement