本文整理汇总了Golang中github.com/martini-contrib/binding.Errors.Add方法的典型用法代码示例。如果您正苦于以下问题:Golang Errors.Add方法的具体用法?Golang Errors.Add怎么用?Golang Errors.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/martini-contrib/binding.Errors
的用法示例。
在下文中一共展示了Errors.Add方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Validate
// Validate the RegisterModel instance
func (vm RegisterViewModel) Validate(errors binding.Errors, req *http.Request) binding.Errors {
vm.Account.Name = strings.TrimSpace(vm.Account.Name)
vm.Account.ContactName = strings.TrimSpace(vm.Account.ContactName)
vm.Account.ContactEmail = strings.TrimSpace(vm.Account.ContactEmail)
vm.Account.ContactPhoneNumber = strings.TrimSpace(vm.Account.ContactPhoneNumber)
vm.Account.Host = strings.TrimSpace(vm.Account.Host)
if len(vm.Account.Host) > 20 {
errors.Add([]string{"host"}, "", "The host must be smaller than 20 characters")
}
if len(vm.Account.Name) < 3 || len(vm.Account.Name) > 100 {
errors.Add([]string{"name"}, "", "The account name must be at least 3 characters or smaller than 255")
}
if len(vm.Account.ContactName) < 3 {
errors.Add([]string{"contact_name"}, "", "The contact name must be at least 3 characters or smaller than 255")
}
if len(vm.Account.ContactPhoneNumber) < 10 || len(vm.Account.ContactPhoneNumber) > 50 {
errors.Add([]string{"contact_phone_number"}, "", "A valid phone number must be provided")
}
_, err := mail.ParseAddress(vm.Account.ContactEmail)
if len(vm.Account.ContactEmail) < 3 || err != nil {
errors.Add([]string{"email"}, "", "A valid email address must be provided")
}
return errors
}
示例2: AccountSave
// AccountSave renders user's account page
func AccountSave(r render.Render, tokens oauth2.Tokens, session sessions.Session, username UsernameForm, err binding.Errors) {
pd := NewPageData(tokens, session)
if len(username.Username) > 0 {
existingUser := &User{Username: username.Username}
db.Where(existingUser).First(existingUser)
if existingUser.ID > 0 {
err.Add([]string{"username"}, "RequiredError", "This username is already taken.")
}
log.Printf("[AccountSave] existing user: %s", existingUser)
}
if err.Len() == 0 {
user := pd.User
if user.Username != username.Username {
path := filepath.Join(HomeDir(), "app/public/files/"+user.Username)
if len(user.Username) == 0 {
path = filepath.Join(HomeDir(), "app/public/files/"+username.Username)
}
if _, err := os.Stat(path); os.IsNotExist(err) || len(user.Username) == 0 {
os.Mkdir(path, 0755)
}
if _, err := os.Stat(path); err == nil {
newPath := filepath.Join(HomeDir(), "app/public/files/"+username.Username)
os.Rename(path, newPath)
}
user.Username = username.Username
db.Save(user)
}
session.AddFlash("You have successfully updated your BareCV username / domain.", "success")
r.Redirect(config.AppUrl+"/account", 302)
} else {
pd.Errors = &err
log.Printf("[AccountSave] errors: %s", err[0].Classification)
r.HTML(200, "account", pd)
}
}