You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
2.6 KiB
44 lines
2.6 KiB
from apps.user.models import * |
|
from django import forms |
|
from django.core.exceptions import ValidationError |
|
import re |
|
|
|
|
|
def mobile_validate(value): |
|
mobile_re = re.compile(r'/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/') |
|
if not mobile_re.match(value): |
|
raise ValidationError('手机号码格式错误') |
|
|
|
|
|
class UserRegForm(forms.Form): |
|
username = forms.CharField(label='用户名', min_length=6, widget=forms.widgets.TextInput( |
|
attrs={'class': 'form-control', 'placeholder': '请输入用户名'}), |
|
error_messages={'required': '用户名不能为空', |
|
'min_length': '长度最少6位'}) |
|
password = forms.CharField(label='密码', min_length=6, max_length=12, |
|
widget=forms.widgets.PasswordInput(attrs={'class': 'form-control'}, render_value=True), |
|
error_messages={'max_length': '密码最大12位', |
|
'min_length': '密码最少6位', |
|
'required': '密码不能为空'}) |
|
re_password = forms.CharField(label='确认密码', min_length=6, max_length=12, |
|
widget=forms.widgets.PasswordInput(attrs={'class': 'form-control'}, |
|
render_value=True), |
|
error_messages={'max_length': '密码最大12位', |
|
'min_length': '密码最少6位', |
|
'required': '密码不能为空'}) |
|
email = forms.CharField(label='邮箱', widget=forms.widgets.EmailInput(attrs={'class': 'form-control'}), |
|
error_messages={'required': '邮箱不能为空', |
|
'invalid': '邮箱格式不正确'}) |
|
mobile = forms.CharField(label='手机号', validators=[mobile_validate], |
|
widget=forms.widgets.TextInput(attrs={'class': 'form-control'}), |
|
error_messages={'required': '手机号不能为空'}) |
|
user_img = forms.ImageField(label='用户头像', required=False, |
|
widget=forms.widgets.FileInput(attrs={'class': 'form-control'})) |
|
|
|
# 全局钩子函数 |
|
def clean(self): |
|
password = self.cleaned_data.get('password') |
|
re_password = self.cleaned_data.get('re_password') |
|
print(password) |
|
if password != re_password: |
|
self.add_error('re_password', ValidationError('两次密码不一致'))
|
|
|