func (c Accounts) LoginCreate() revel.Result { u := User{} newUser := User{} v := revel.Validation{} if !c.validateCaptcha(c.Params.Get("captcha")) { v.Error("验证码不正确") return c.renderValidation("accounts/login.html", v) } newUser, v = u.Signin(c.Params.Get("login"), c.Params.Get("password")) if v.HasErrors() { return c.renderValidation("accounts/login.html", v) } c.storeUser(&newUser) c.Flash.Success("登录成功,欢迎再次回来。") return c.Redirect(Home.Index) }
func (s SearchForm) Validate(v *revel.Validation, locale string) { if str := strings.Trim(s.Title, " "); str != "" { v.MinSize(str, 6).Message(revel.Message(locale, "search.form.validate.title.min", 6)).Key("s.Title") } if str := strings.Trim(s.Runtime, " "); str != "" { rt, err := strconv.Atoi(str) if err != nil { v.Error(revel.Message(locale, "search.form.validate.runtime.number")).Key("s.Runtime") } else { v.Range(rt, 50, 120).Message(revel.Message(locale, "search.form.validate.runtime.range")).Key("s.Runtime") } } if str := strings.Trim(s.OriginalAirDate, " "); str != "" { v.Match(str, regexp.MustCompile("^(January|February|March|April|May|June|July|August|September|October|November|December).*$")).Message(revel.Message(locale, "search.form.validate.originalairdate.match")).Key("s.OriginalAirDate") } }
// ValidateCard validates whether the card's values are appropriate // when creating a new card. func ValidateCard(validator *revel.Validation, params url.Values) { validator.Clear() id, err := strconv.ParseInt(params.Get("CreatorID"), 0, 0) if err != nil { validator.Error("The ID of the creator should be a number").Key("creatorID") } creatorID := int(id) cardBody := params.Get("CardBody") t, err := strconv.ParseInt(params.Get("CardType"), 0, 0) if err != nil { validator.Error("Card Type should be a number").Key("cardType") } cardType := int(t) b, err := strconv.ParseInt(params.Get("CardBlanks"), 0, 0) if err != nil { validator.Error("Card Blanks should be a number").Key("cardBlanks") } cardBlanks := int(b) validator.Required(cardBody) validator.Range(int(cardType), 0, 1).Message("The card type can only be 0 for a white card, or 1 for a black card.") validator.Range(int(cardBlanks), 0, 3).Message("Card blanks must be in the range of 0 - 3.") if cardType == 0 { validator.Max(int(cardBlanks), 0).Message("You cannot have blank spaces in a card unless it is a black card.)") } validator.Min(creatorID, 0).Message("The creator ID must be greater than 0.") }
func ValidateProfileName(v *revel.Validation, name string) *revel.ValidationResult { result := v.Required(name).Message("Name required") if !result.Ok { return result } result = v.MinSize(name, 2).Message("Name must be at least 2 characters") if !result.Ok { return result } result = v.MaxSize(name, 100).Message("Name must be at most 100 characters") if !result.Ok { return result } // Inverse regexp matcher (name cannot contain reserved # or @ symbols) if invalidNameMatcher := NameRegex.FindString(name); invalidNameMatcher == "" { result = v.Error("Invalid Name. Reserved characters ('#' and '@') are not allowed") } return result }
func ValidateProfileUserName(v *revel.Validation, username string) *revel.ValidationResult { result := v.Required(username).Message("User name required") if !result.Ok { return result } result = v.MaxSize(username, 64).Message("User name can not exceed 64 characters") if !result.Ok { return result } result = v.Match(username, UserNameRegex).Message("Invalid User name. Alphanumerics allowed only") if !result.Ok { return result } // Inverse regexp matcher (username cannot be the same as any blacklisted usernames) if blacklistMatcher := UserNameBlacklistRegex.FindString(username); blacklistMatcher != "" { result = v.Error("Invalid User name. Reserved keywords not allowed") } return result }