|
|
|
from django import forms
|
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from .models import *
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
|
|
def age_validate(age):
|
|
|
|
if age <= 0 or age > 150:
|
|
|
|
raise ValidationError('年龄错误')
|
|
|
|
|
|
|
|
|
|
|
|
def mobile_validate(mobile):
|
|
|
|
mobile_re = re.compile(r'^1[3-9]]\d{9}$')
|
|
|
|
if not mobile_re.match(mobile):
|
|
|
|
raise ValidationError('手机号格式错误')
|
|
|
|
|
|
|
|
|
|
|
|
# 用户信息表单
|
|
|
|
class UserInfoForm(forms.Form):
|
|
|
|
STATUS = ((None, '请选择'), (0, '正常'), (1, '无效'))
|
|
|
|
username = forms.CharField(label="用户名", min_length=2, widget=forms.widgets.TextInput(
|
|
|
|
attrs={'class': 'form-control', 'placeholder': '请输入用户名'}),
|
|
|
|
error_messages={'required': '用户姓名不能为空', 'min_length': '长度最小6位',
|
|
|
|
'invalid': '请输入正确用户名'})
|
|
|
|
password = forms.CharField(label='密码', min_length=6, max_length=10,
|
|
|
|
widget=forms.widgets.PasswordInput(attrs={'class': 'password'}, render_value=True),
|
|
|
|
help_text='密码长度为6-10个字符', error_messages={'required': '密码不能为空'})
|
|
|
|
age = forms.IntegerField(label='年龄', initial=1, min_value=1, max_value=150, validators=[age_validate],
|
|
|
|
error_messages={'required': '年龄不能为空'})
|
|
|
|
mobile = forms.CharField(label='手机号码', validators=[mobile_validate],
|
|
|
|
error_messages={'required': '手机号不能为空'})
|
|
|
|
# status = forms.ChoiceField(label='状态', choices=STATUS, widget=forms.widgets.Select,
|
|
|
|
# error_messages={'required': '状态不能为空'})
|
|
|
|
status = forms.ModelChoiceField(label='状态', queryset=UserStatus.objects.all(), empty_label='请选择',
|
|
|
|
error_messages={'required': '状态不能为空'})
|
|
|
|
createdate = forms.DateField(label='创建日期', required=False, input_formats=["%Y-%m-%d"])
|