func (user *User) Validate(v *revel.Validation) { v.Required(user.Name).Message("请输入用户名") valid := v.Match(user.Name, nameRegex).Message("只能使用字母、数字和下划线") if valid.Ok { if user.HasName() { err := &revel.ValidationError{ Message: "该用户名已经注册过", Key: "user.Name", } valid.Error = err valid.Ok = false v.Errors = append(v.Errors, err) } } v.Required(user.Email).Message("请输入Email") valid = v.Email(user.Email).Message("无效的电子邮件") if valid.Ok { if user.HasEmail() { err := &revel.ValidationError{ Message: "该邮件已经注册过", Key: "user.Email", } valid.Error = err valid.Ok = false v.Errors = append(v.Errors, err) } } v.Required(user.Password).Message("请输入密码") v.MinSize(user.Password, 3).Message("密码最少三位") v.Required(user.ConfirmPassword == user.Password).Message("密码不一致") }
func (a *Admin) Validate(v *revel.Validation) { v.Required(a.Username).Message("请输入用户名!") valid := v.Match(a.Username, regexp.MustCompile("^\\w*$")).Message("只能使用字母、数字和下划线!") if valid.Ok { if a.HasName() { err := &revel.ValidationError{ Message: "该用户名已经注册过!", Key: "a.Username", } valid.Error = err valid.Ok = false v.Errors = append(v.Errors, err) } } v.Required(a.Email).Message("请输入Email") valid = v.Email(a.Email).Message("无效的电子邮件!") if valid.Ok { if a.HasEmail() { err := &revel.ValidationError{ Message: "该邮件已经注册过!", Key: "a.Email", } valid.Error = err valid.Ok = false v.Errors = append(v.Errors, err) } } v.Required(a.Password).Message("请输入密码!") v.MinSize(a.Password, 3).Message("密码最少三位!") }
func (category Category) Validate(v *revel.Validation) { v.Required(category.Name).Message("请输入名称") if category.HasName() { err := &revel.ValidationError{ Message: "名称已存在", Key: "category.Name", } v.Errors = append(v.Errors, err) } }
func checkImageExt(v *revel.Validation, file *multipart.File, header *multipart.FileHeader, formField string) bool { if !strings.Contains(IMAGE_EXTS, strings.ToLower(path.Ext(header.Filename))) { err := &revel.ValidationError{ Message: "只能上传图片", Key: formField, } v.Errors = append(v.Errors, err) return false } if (*file).(Sizer).Size() > IMAGE_LIMIT_SIZE { err := &revel.ValidationError{ Message: fmt.Sprintf("图片尺寸不超过%dKB", IMAGE_LIMIT_SIZE), Key: formField, } v.Errors = append(v.Errors, err) return false } return true }
func checkFileExt(v *revel.Validation, header *multipart.FileHeader, fileExts, formField, message string) bool { if !strings.Contains(fileExts, strings.ToLower(path.Ext(header.Filename))) { err := &revel.ValidationError{ Message: message, Key: formField, } v.Errors = append(v.Errors, err) return false } return true }
func (topic Topic) Validate(v *revel.Validation) { v.Required(topic.Title).Message("请输入标题") if utf8.RuneCountInString(topic.Title) > 35 { err := &revel.ValidationError{ Message: "最多35个字", Key: "topic.Title", } v.Errors = append(v.Errors, err) } v.Required(topic.Category).Message("请选择分类") v.Required(topic.Content).Message("帖子内容不能为空") }
func (product Product) Validate(v *revel.Validation) { v.Required(product.Name).Message("不能为空") v.Required(product.Author).Message("不能为空") v.Required(product.Description).Message("不能为空") if utf8.RuneCountInString(product.Name) > 20 { err := &revel.ValidationError{ Message: "最多20个字", Key: "product.Name", } v.Errors = append(v.Errors, err) } }
func (category *Category) Validate(v *revel.Validation) { valid := v.Required(category.Name).Message("请输入名称") if valid.Ok { if category.HasName() { err := &revel.ValidationError{ Message: "该名称已存在", Key: "category.Name", } valid.Error = err valid.Ok = false v.Errors = append(v.Errors, err) } } }
//校验 func (user *User) Validation(q *qbs.Qbs, v *revel.Validation) { v.Required(user.UserName).Message("请输入用户名") v.Required(user.Email).Message("请输入邮箱") v.Required(user.Password).Message("请输入密码") valid := v.Email(user.Email).Message("邮箱格式错误") if valid.Ok { if user.HasEmail(q) { err := &revel.ValidationError{ Message: "该邮件已经注册过", Key: "user.Email", } valid.Error = err valid.Ok = false v.Errors = append(v.Errors, err) } } v.Required(user.Password == user.AffirmPassword).Message("两次密码输入不一致") }