本文整理汇总了Golang中github.com/astaxie/beego/validation.Validation.SetError方法的典型用法代码示例。如果您正苦于以下问题:Golang Validation.SetError方法的具体用法?Golang Validation.SetError怎么用?Golang Validation.SetError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/astaxie/beego/validation.Validation
的用法示例。
在下文中一共展示了Validation.SetError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Valid
func (form *ResetPwdForm) Valid(v *validation.Validation) {
// Check if passwords of two times are same.
if form.Password != form.PasswordRe {
v.SetError("PasswordRe", "Passwords of two times are not matched.")
return
}
}
示例2: Valid
func (form *PostAdminForm) Valid(v *validation.Validation) {
user := models.User{Id: form.User}
if user.Read() != nil {
v.SetError("User", "admin.not_found_by_id")
}
user.Id = form.LastReply
if user.Read() != nil {
v.SetError("LastReply", "admin.not_found_by_id")
}
user.Id = form.LastAuthor
if user.Read() != nil {
v.SetError("LastReply", "admin.not_found_by_id")
}
topic := models.Topic{Id: form.Topic}
if topic.Read() != nil {
v.SetError("Topic", "admin.not_found_by_id")
}
if len(i18n.GetLangByIndex(form.Lang)) == 0 {
v.SetError("Lang", "Not Found")
}
}
示例3: Check
func (u *User) Check(v *validation.Validation) {
if u.Name == "toukii" && u.Passwd == "1234" {
v.Clear()
} else {
v.SetError("login", "user is abnormal.")
}
}
示例4: Valid
func (form *LoginForm) Valid(v *validation.Validation) {
valid := validation.Validation{}
valid.Email(form.Email, "email")
if valid.HasErrors() {
v.SetError("Email", form.Locale.Tr("valid.invalid_email"))
}
}
示例5: Valid
// Valid is the custom validation. It runs only when all the standard validations pass
func (o *Object) Valid(v *validation.Validation) {
if o.Score == 0 {
// We have no way to set error code here
// TODO: fix it in Beego
v.SetError("Score", "Can not be empty")
}
}
示例6: Valid
// Valid ...
func (r *RepTarget) Valid(v *validation.Validation) {
if len(r.Name) == 0 {
v.SetError("name", "can not be empty")
}
if len(r.Name) > 64 {
v.SetError("name", "max length is 64")
}
if len(r.URL) == 0 {
v.SetError("endpoint", "can not be empty")
}
r.URL = utils.FormatEndpoint(r.URL)
if len(r.URL) > 64 {
v.SetError("endpoint", "max length is 64")
}
// password is encoded using base64, the length of this field
// in DB is 64, so the max length in request is 48
if len(r.Password) > 48 {
v.SetError("password", "max length is 48")
}
}
示例7: processUserPasswordForm
func (this *UserController) processUserPasswordForm(user *models.User) {
valid := validation.Validation{}
userPasswordForm := UserPasswordForm{}
if err := this.ParseForm(&userPasswordForm); err != nil {
beego.Error(err)
}
_, err := valid.Valid(userPasswordForm)
if err != nil {
beego.Error(err)
this.Abort("400")
}
if !user.VerifyPassword(userPasswordForm.CurrentPassword) {
valid.SetError("CurrentPassword", "当前密码错误")
}
if len(valid.Errors) > 0 {
this.Data["UserPasswordFormValidErrors"] = valid.Errors
beego.Trace(fmt.Sprint(valid.Errors))
} else {
user.SetPassword(userPasswordForm.Password)
if err := user.Update(); err != nil {
this.Abort("500")
}
this.FlashWrite("notice", "密码已更新!")
this.Redirect(this.Ctx.Request.RequestURI, 302)
}
}
示例8: Valid
func (model *ResetPwdModel) Valid(v *validation.Validation) {
// Check if passwords of two times are same.
if model.Password != model.PasswordRe {
v.SetError("PasswordRe", "auth.repassword_not_match")
return
}
}
示例9: Valid
func (form *CommentAdminForm) Valid(v *validation.Validation) {
var err error
if _, err = models.GetUserById(int64(form.User)); err != nil {
v.SetError("User", "admin.not_found_by_id")
}
if _, err = models.GetPostById(int64(form.Post)); err != nil {
v.SetError("Post", "admin.not_found_by_id")
}
}
示例10: Valid
func (form *CategoryAdminForm) Valid(v *validation.Validation) {
category := models.Category{Name: form.Name}
if models.GetByExample(&category); category.Id != int64(form.Id) {
v.SetError("Name", "admin.field_need_unique")
}
category = models.Category{Slug: form.Slug}
if models.GetByExample(&category); category.Id != int64(form.Id) {
v.SetError("Slug", "admin.field_need_unique")
}
}
示例11: Valid
func (form *CategoryAdminForm) Valid(v *validation.Validation) {
qs := models.Categories()
if models.CheckIsExist(qs, "Name", form.Name, form.Id) {
v.SetError("Name", "admin.field_need_unique")
}
if models.CheckIsExist(qs, "Slug", form.Slug, form.Id) {
v.SetError("Slug", "admin.field_need_unique")
}
}
示例12: Valid
func (form *PasswordForm) Valid(v *validation.Validation) {
// Check if passwords of two times are same.
if form.Password != form.PasswordRe {
v.SetError("PasswordRe", "auth.repassword_not_match")
return
}
if VerifyPassword(form.PasswordOld, form.User.Password) == false {
v.SetError("PasswordOld", "auth.old_password_wrong")
}
}
示例13: ForgetPassword
//输入email,发送重设密码邮件
func (this *AuthController) ForgetPassword() {
this.Data["PageTitle"] = fmt.Sprintf("忘记密码 | %s", setting.AppName)
this.Layout = "layout.html"
this.TplNames = "forget-password.html"
valid := validation.Validation{}
form := ForgetPasswordForm{}
if this.Ctx.Request.Method == "POST" {
if err := this.ParseForm(&form); err != nil {
beego.Error(err)
}
_, err := valid.Valid(form)
if err != nil {
beego.Error(err)
}
user := models.User{Email: form.Email}
if err := user.Read("Email"); err != nil {
beego.Trace(user)
beego.Trace(form)
valid.SetError("Email", "此电子邮件并未注册")
}
beego.Trace(valid.Errors)
if len(valid.Errors) == 0 {
//发送忘记密码邮件
code, err := user.GenerateActivateCode()
if err != nil {
this.Abort("500")
}
sub := sendcloud.NewSubstitution()
sub.AddTo(user.Email)
sub.AddSub("%appname%", setting.AppName)
sub.AddSub("%name%", user.Username)
sub.AddSub("%url%", setting.AppUrl+beego.UrlFor("AuthController.ResetPassword", ":code", code))
if err := setting.Sendcloud.SendTemplate("gotalk_password", setting.AppName+"忘记密码", setting.From, setting.FromName, sub); err != nil {
beego.Error(err)
}
this.FlashWrite("notice", fmt.Sprintf("重设密码的方法已经发到%s。请查收!", user.Email))
this.Redirect("/", 302)
} else {
this.Data["HasError"] = true
this.Data["errors"] = valid.Errors
}
}
}
示例14: processUploadAvatar
func (this *UserController) processUploadAvatar(user *models.User) {
valid := validation.Validation{}
avatarFile, header, err := this.GetFile("Avatar")
if err != nil {
this.Abort("400")
}
err = user.ValidateAndSetAvatar(avatarFile, header.Filename)
defer avatarFile.Close()
if err != nil {
valid.SetError("Avatar", err.Error())
this.Data["UserAvatarFormValidErrors"] = valid.Errors
} else {
if err := user.Update("Avatar"); err != nil {
this.Abort("500")
}
this.FlashWrite("notice", "头像已更新!")
this.Redirect(this.Ctx.Request.RequestURI, 302)
}
}
示例15: DoRegister
func (this *AuthController) DoRegister() {
this.CheckRequestFrequency(3, 15, 30)
this.Layout = "layout.html"
this.TplNames = "register.html"
valid := validation.Validation{}
form := RegisterForm{}
if err := this.ParseForm(&form); err != nil {
beego.Error(err)
}
b, err := valid.Valid(form)
if err != nil {
beego.Error(err)
}
if !b {
this.registerPageWithErrors(form, valid.Errors)
return
}
//验证用户名
user := models.User{Username: form.Username}
if err := user.ValidUsername(); err != nil {
valid.SetError("username", err.Error())
this.registerPageWithErrors(form, valid.Errors)
return
} else {
if user.Read("Username") == nil {
valid.SetError("username", fmt.Sprintf("%s已被使用,请使用其他用户名!", form.Username))
this.registerPageWithErrors(form, valid.Errors)
return
}
}
//验证email未被注册
user.Email = form.Email
if user.Read("Email") == nil {
valid.SetError("email", "已被使用,请直接使用此电邮登录")
this.registerPageWithErrors(form, valid.Errors)
return
}
//通过所有验证
actCode, _ := user.GenerateActivateCode()
user.SetPassword(form.Password)
if err := user.Insert(); err != nil {
beego.Error(err)
this.Abort("500")
return
}
sub := sendcloud.NewSubstitution()
sub.AddTo(user.Email)
sub.AddSub("%appname%", setting.AppName)
sub.AddSub("%name%", user.Username)
sub.AddSub("%url%", setting.AppUrl+beego.UrlFor("AuthController.Activate", ":code", actCode))
if err := setting.Sendcloud.SendTemplate("gotalk_register", setting.AppName+"欢迎你", setting.From, setting.FromName, sub); err != nil {
beego.Error(err)
}
this.FlashWrite("notice", fmt.Sprintf("注册成功!欢迎你, %s。建议你再花点时间上传头像、验证电子邮件!", user.Username))
this.LogUserIn(&user, false)
userEditUrl := beego.UrlFor("UserController.Edit", ":username", user.Username)
this.Redirect(userEditUrl, 302)
return
}