Exemple #1
0
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("密码最少三位!")
}
Exemple #2
0
func (user User) Validate(v *revel.Validation) {
	v.Required(user.Name).Message("请输入用户名")
	v.Match(user.Name, nameRegex).Message("只能使用字母、数字和下划线")

	if user.HasName() {
		err := &revel.ValidationError{
			Message: "用户名已经注册过",
			Key:     "user.Name",
		}
		v.Errors = append(v.Errors, err)
	}

	v.Required(user.Email).Message("请输入Email")
	v.Email(user.Email).Message("无效的电子邮件")

	if user.HasEmail() {
		err := &revel.ValidationError{
			Message: "邮件已经注册过",
			Key:     "user.Email",
		}
		v.Errors = append(v.Errors, err)
	}

	v.Required(user.Password).Message("请输入密码")
	v.MinSize(user.Password, 3).Message("密码最少三位")
	v.Required(user.ConfirmPassword == user.Password).Message("密码不一致")
}
Exemple #3
0
func (p *Pagination) isValidPage(v *revel.Validation,
	bean interface{}, index ...int64) {
	n, err := engine.Count(bean)
	if err != nil {
		e := &revel.ValidationError{
			Message: "bean error",
			Key:     reflect.TypeOf(bean).Name(),
		}
		v.Errors = append(v.Errors, e)
	}
	//modify n to 0
	if n < 0 {
		n = 0
	}
	//current page number
	var c int64
	if len(index) == 0 {
		c = 1
	} else {
		c = index[0]
		if c == 0 {
			c = 1
		}
	}
	if c*perPage > n+perPage || c < 1 {
		e := &revel.ValidationError{
			Message: fmt.Sprintf("%d is out of range %d",
				c, n/perPage),
			Key: reflect.TypeOf(bean).Name(),
		}
		v.Errors = append(v.Errors, e)
	}
}
Exemple #4
0
func (user *User) Validate(v *revel.Validation) {
	v.Required(user.Name)
	v.Required(user.Email)
	valid := v.Email(user.Email)
	if user.Password != user.ConfirmPassword {
		v.Errors = append(v.Errors, &revel.ValidationError{Message: "两次密码不一致", Key: "user.Password"})
	}

	if user.HasName() {
		fmt.Println("has name")
		err := &revel.ValidationError{
			Message: "该用户名已经被注册",
			Key:     "user.Name",
		}
		v.Errors = append(v.Errors, err)
	}

	if valid.Ok {
		if user.HasEmail() {
			fmt.Println("has email")
			err := &revel.ValidationError{
				Message: "该邮箱已经被用于注册",
				Key:     "user.Email",
			}
			v.Errors = append(v.Errors, err)
		}
	}
}
Exemple #5
0
func ValidationAddErr(v *revel.Validation, message string) {
	v.Errors = append(v.Errors,
		&revel.ValidationError{
			Message: message,
			Key:     "Err",
		},
	)
}
Exemple #6
0
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)
	}
}
Exemple #7
0
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
}
Exemple #8
0
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
}
Exemple #9
0
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("帖子内容不能为空")
}
Exemple #10
0
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)
	}
}