当前位置: 首页>>代码示例>>Golang>>正文


Golang Validation.Required方法代码示例

本文整理汇总了Golang中github.com/astaxie/beego/validation.Validation.Required方法的典型用法代码示例。如果您正苦于以下问题:Golang Validation.Required方法的具体用法?Golang Validation.Required怎么用?Golang Validation.Required使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/astaxie/beego/validation.Validation的用法示例。


在下文中一共展示了Validation.Required方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: ValidUserAdd

// Valid user add
func (u *User) ValidUserAdd() *validation.Validation {
	valid := validation.Validation{}

	u.Username = strings.TrimSpace(u.Username)
	u.Password = strings.TrimSpace(u.Password)

	valid.Required(u.Username, "UsernameReq").Message("用户名不能为空")
	valid.MinSize(u.Username, 6, "UsernameMixSize").Message("用户名不能小于6个字符")
	valid.MaxSize(u.Username, 10, "UsernameMaxSize").Message("用户姓名不能大于10个字符")

	valid.MinSize(u.Password, 6, "PasswordMinSize").Message("用户密码不能小于6为字符")
	valid.MaxSize(u.Password, 10, "PasswordMaxSize").Message("用户密码不能大于10个字符")

	// uLen := len(strings.TrimSpace(u.Username))
	// if uLen < 6 || uLen > 10 {
	// valid.SetError("Username", "用户不能为空,长度大于6小于10")
	// }

	// pLen := len(strings.TrimSpace(u.Password))
	// if pLen < 6 || pLen > 10 {
	// valid.SetError("Password", "用户密码不能为空,长度大于6小于10")
	// }

	return &valid
}
开发者ID:11101171,项目名称:violence,代码行数:26,代码来源:user.go

示例2: Login

/**
 * 登录
 */
func (this *LoginController) Login() {
	val := validation.Validation{}
	username := this.GetString("username")
	password := this.GetString("password")

	val.Required(username, "username")
	val.Required(password, "password")

	if val.HasErrors() {
		for _, v := range val.Errors {
			this.Ctx.WriteString("<script>alert('" + v.Key + " " + v.Message + "');</script>") //遇到一个错误就可以StopRun了其实,这里只是练习验证模块
		}
		this.StopRun()
	}
	toolbox.Display("u", username)
	toolbox.Display("pa", password)

	password = utils.LoginPassword(password)
	toolbox.Display("paaa", password)

	if user.CheckLogin(username, password) == true {
		this.SetSession("username", username)
		this.Ctx.SetCookie("username", username)
		this.Redirect("/admin", 302)
	} else {
		this.Redirect("/login", 302)
	}
}
开发者ID:xiaoyang2022,项目名称:goBlog,代码行数:31,代码来源:login.go

示例3: EditUser

func (this *AdminController) EditUser() {
	o := orm.NewOrm()
	o.Using("default")

	usersId, _ := strconv.Atoi(this.Ctx.Input.Param(":id"))
	users := models.User{}

	flash := beego.NewFlash()

	err := o.QueryTable("user").Filter("id", usersId).One(&users)

	if err != orm.ErrNoRows {

		err := this.ParseForm(&users)
		if err != nil {

			beego.Error("Impossible de parser. Raison: ", err)

		} else {

			valid := validation.Validation{}

			valid.Required(users.Mail, "mail")
			valid.Required(users.Role, "role")

			isValid, _ := valid.Valid(users)

			if this.Ctx.Input.Method() == "POST" {

				if !isValid {
					flash.Error("Impossible de mettre à jour l'utilisateur")
					flash.Store(&this.Controller)
					this.Redirect("/incident-manager/admin/user", 302)
				} else {
					_, err := o.Update(&users)

					if err == nil {
						flash.Notice("Utilisateur " + users.Mail + " mis à jour")
						flash.Store(&this.Controller)

						this.Redirect("/incident-manager/admin/user", 302)
					} else {
						fmt.Println("erreur")

						beego.Debug("Mise à jour Impossible dû a : ", err)
					}
				}

			}

		}
		this.Redirect("/incident-manager/admin/user", 302)

	} else {
		flash.Notice("Utilisateur %d n'existe pas", usersId)
		flash.Store(&this.Controller)
		this.Redirect("/incident-manager/", 302)
	}

}
开发者ID:kameleon83,项目名称:managIncident,代码行数:60,代码来源:user.go

示例4: TestPost

/* 测试战斗提交表单 */
func (this *ApiController) TestPost() {
	//测试提交的表单
	valid := validation.Validation{}
	valid.Required(this.GetString("my"), "1")
	valid.Match(this.GetString("my"), regexp.MustCompile("^[,0-9]+$"), "2")
	valid.Required(this.GetString("enemie"), "3")
	valid.Match(this.GetString("enemie"), regexp.MustCompile("^[,0-9]+$"), "4")
	if valid.HasErrors() { //没有通过验证则退出
		return
	}
	//解析提交的敌我方数据
	myArray := strings.Split(this.GetString("my"), ",")
	enemieArray := strings.Split(this.GetString("enemie"), ",")
	mySlice := make([]string, len(myArray))
	enemieSlice := make([]string, len(enemieArray))
	for k, v := range myArray {
		mySlice[k] = v + ",0,0,0,0,0"
	}
	for k, v := range enemieArray {
		enemieSlice[k] = v + ",0,0,0,0,0"
	}
	//模拟战斗
	index := IndexController{}
	result := index.SimulateFight(mySlice, "0;0", enemieSlice, "0;0")
	this.Data["json"] = result
	this.ServeJson()
}
开发者ID:cnhans,项目名称:kingdoms,代码行数:28,代码来源:api.go

示例5: Signup

/**
 * 登录接收
 */
func (this *UserController) Signup() {
	//errmsg := make(map[string]string)
	requestEmail := this.GetString("email")
	requestPassword := this.GetString("password")

	svalid := validation.Validation{}

	svalid.Required(requestEmail, "email")
	svalid.MaxSize(requestEmail, 40, "email")
	svalid.Required(requestPassword, "password")

	if svalid.HasErrors() {
		for _, err := range svalid.Errors {
			//fmt.Println(err.Key, err.Message)
			this.showmsg("Error Message", "["+err.Key+"]"+err.Message)
		}
	}

	var user models.User
	user.Email = requestEmail
	user.Password = models.Md5([]byte(requestPassword))

	var Ru models.ReturnUser
	Ru = user.Select()

	if Ru.Id > 0 {
		this.SetSession("nickname", Ru.Nickname)
		this.SetSession("email", Ru.Email)
		this.SetSession("isadmin", Ru.Isadmin)
		this.Redirect("/", 302)
	} else {
		this.showmsg("Error", "Email OR Password IS WRONG")
	}
}
开发者ID:lvzhongyi,项目名称:canku,代码行数:37,代码来源:user.go

示例6: Validators

func (this *Article) Validators(validator *validation.Validation, scenario string) {
	if scenario == "create" || scenario == "update" {
		validator.Required(this.Description, "description")
		validator.Min(this.Description, 0, "description")
		validator.Max(this.Description, 12000, "description")
		validator.Max(this.Title, 3, "title")
		validator.Min(this.Title, 255, "title")
	}
}
开发者ID:nizsheanez,项目名称:ria,代码行数:9,代码来源:article.go

示例7: Register

func (this *RegisterController) Register() {

	o := orm.NewOrm()
	o.Using("default")

	register := models.Register{}
	flash := beego.NewFlash()
	// this.Data["Form"] = &register

	if err := this.ParseForm(&register); err != nil {
		beego.Error("Couldn't parse the form. Reason: ", err)
	} else {
		valid := validation.Validation{}

		valid.Required(register.Mail, "mail")

		isValid, _ := valid.Valid(register)

		if this.Ctx.Input.Method() == "POST" {

			if !isValid {
				this.Data["errors"] = valid.ErrorsMap

				for _, err := range valid.Errors {
					beego.Error(err.Key, err.Message)
				}

			} else {
				r := this.Ctx.Input
				register.IP = r.IP()
				fmt.Println(register.IP)

				_, err := o.Insert(&register)

				// res, err := o.Raw("INSERT INTO `incident` (`cat`, `title`, `description`,`date_request`, `priority`, `user_id`) VALUES (?,?,?,?,?,?)", register.Cat, register.Title, register.Description, date, register.Priority, this.GetSession("uid")).Exec()
				if err == nil {
					err := admin.SendMailAdmin()
					if err != nil {
						fmt.Println(err)
					}
					flash.Success(register.Mail + " : votre demande a bien été envoyé ")
					flash.Store(&this.Controller)
					this.Redirect("/", 302)
				} else {
					flash.Warning("Attention car cette adresse mail : " + register.Mail + " est déjà dans les demandes. ")
					flash.Store(&this.Controller)
					beego.Debug("Couldn't insert in tableName Register. Reason: ", err)

				}
			}

		}

	}
	Template(this, "user", "register", "Demande de Connexion / Nouveau mot de passe")
}
开发者ID:kameleon83,项目名称:managIncident,代码行数:56,代码来源:register.go

示例8: ValidCmdAdd

// Valid cmdForm add
func (this *Cmd) ValidCmdAdd() *validation.Validation {
	valid := validation.Validation{}

	valid.Required(this.Shell, "ShellReq").Message("脚本不能为空")
	valid.MinSize(this.Shell, 1, "ShellMixSize").Message("脚本不能小于1个字符")
	valid.MaxSize(this.Shell, 225, "ShellMaxSize").Message("脚本不能大于225个字符")

	valid.Required(this.AgentId, "AgentIdReq").Message("主机不能为空")

	return &valid
}
开发者ID:11101171,项目名称:violence,代码行数:12,代码来源:cmd.go

示例9: ValidCompany

//表单验证
func ValidCompany(m *BCompany) string {
	v := validation.Validation{}
	v.Required(m.Name, "name").Message("单位名称不能为空")
	v.Required(m.Phone, "phone").Message("电话不能为空")
	v.Required(m.Address, "address").Message("地址不能为空")
	if v.HasErrors() {
		// 如果有错误信息,证明验证没通过
		return utils.WrapErrors(v.Errors)
	}
	return ""
}
开发者ID:sndnvaps,项目名称:gooa,代码行数:12,代码来源:validation.go

示例10: ValidUser

//表单验证
func ValidUser(m *BUser) string {
	v := validation.Validation{}
	v.Required(m.Username, "username").Message("邮箱不能为空")
	v.Required(m.Password, "password").Message("密码不能为空")
	v.Email(m.Username, "username").Message("邮箱格式不正确")
	if v.HasErrors() {
		// 如果有错误信息,证明验证没通过
		return utils.WrapErrors(v.Errors)
	}
	return ""
}
开发者ID:sndnvaps,项目名称:gooa,代码行数:12,代码来源:validation.go

示例11: Reset

func (this *MainController) Reset() {
	this.activeContent("user/reset")

	flash := beego.NewFlash()

	u := this.Ctx.Input.Param(":uuid")
	o := orm.NewOrm()
	o.Using("default")
	user := models.AuthUser{Reset_key: u}
	err := o.Read(&user, "Reset_key")
	if err == nil {
		if this.Ctx.Input.Method() == "POST" {
			password := this.GetString("password")
			password2 := this.GetString("password2")
			valid := validation.Validation{}
			valid.MinSize(password, 6, "password")
			valid.Required(password2, "password2")
			if valid.HasErrors() {
				errormap := make(map[string]string)
				for _, err := range valid.Errors {
					errormap[err.Key] = err.Message
				}
				this.Data["Errors"] = errormap
				return
			}

			if password != password2 {
				flash.Error("Passwords don't match")
				flash.Store(&this.Controller)
				return
			}
			h := pk.HashPassword(password)

			// Convert password hash to string
			user.Password = hex.EncodeToString(h.Hash) + hex.EncodeToString(h.Salt)

			user.Reset_key = ""
			if _, err := o.Update(&user); err != nil {
				flash.Error("Internal error")
				flash.Store(&this.Controller)
				return
			}
			flash.Notice("Password updated.")
			flash.Store(&this.Controller)
			this.Redirect("/notice", 302)
		}
	} else {
		flash.Notice("Invalid key.")
		flash.Store(&this.Controller)
		this.Redirect("/notice", 302)
	}
}
开发者ID:robfrut135,项目名称:DockerWebConsole,代码行数:52,代码来源:user.go

示例12: ValidAgentAdd

// Valid userForm add
func (a *Agent) ValidAgentAdd() *validation.Validation {
	valid := validation.Validation{}

	a.Host = strings.TrimSpace(a.Host)
	a.Port = strings.TrimSpace(a.Port)
	a.LoginName = strings.TrimSpace(a.LoginName)
	a.LoginPass = strings.TrimSpace(a.LoginPass)

	valid.Required(a.Host, "HostReq").Message("远程地址不能为空")
	valid.MinSize(a.Host, 10, "HostMixSize").Message("远程地址不能小于10个字符")
	valid.MaxSize(a.Host, 225, "HostMaxSize").Message("远程地址不能大于225个字符")

	valid.Required(a.Port, "PortReq").Message("端口不能为空")
	valid.MinSize(a.Port, 2, "PortMixSize").Message("端口不能小于2个字符")
	valid.MaxSize(a.Port, 6, "PortMaxSize").Message("端口不能大于6个字符")

	valid.Required(a.LoginName, "LoginNameReq").Message("登录用户名不能为空")
	valid.MinSize(a.LoginName, 1, "LoginNameMixSize").Message("登录用户名不能小于1个字符")
	valid.MaxSize(a.LoginName, 225, "LoginNameMaxSize").Message("登录用户名不能大于225个字符")

	valid.Required(a.LoginPass, "LoginPassReq").Message("登录密码不能为空")
	valid.MinSize(a.LoginPass, 1, "LoginPassMixSize").Message("登录密码不能小于1个字符")
	valid.MaxSize(a.LoginPass, 225, "LoginPassMaxSize").Message("登录密码不能大于225个字符")

	return &valid
}
开发者ID:11101171,项目名称:violence,代码行数:27,代码来源:agent.go

示例13: Login

func (c *UserController) Login() {
	c.setupView("auth/login")

	if c.Ctx.Input.Method() == "POST" {

		flash := beego.NewFlash()
		username := c.GetString("username")
		password := c.GetString("password")

		valid := validation.Validation{}
		valid.Required(username, "username")
		valid.Required(password, "password")
		if valid.HasErrors() {
			errormap := []string{}
			for _, err := range valid.Errors {
				errormap = append(errormap, "Validation failed on "+err.Key+": "+err.Message+"\n")
			}
			flash.Error("Invalid data!")
			flash.Store(&c.Controller)
			c.Data["Errors"] = errormap
			return
		}

		o := orm.NewOrm()
		o.Using("default")
		user := &models.AuthUser{
			Username: username,
		}

		err := o.Read(user, "Username")
		if err == orm.ErrNoRows {
			flash.Error("User not found!")
			flash.Store(&c.Controller)
			return
		}
		bcryptErr := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))
		if bcryptErr != nil {
			flash.Error("Wrong password!")
			flash.Store(&c.Controller)
			return
		}
		m := make(map[string]interface{})
		m["email"] = user.Email
		m["username"] = user.Username
		m["fullname"] = user.Firstname + " " + user.Lastname
		m["timestamp"] = time.Now()
		c.SetSession("user", m)
		c.Redirect("/nladmin"+c.Ctx.Input.Param(":id"), 302) // go to previous page after login
	}
}
开发者ID:namilabs,项目名称:namilabs-in-beego,代码行数:50,代码来源:user.go

示例14: Create

func Create(post Post) (p Post, err error) {
	o := orm.NewOrm()
	valid := validation.Validation{}
	valid.Required(post.Title, "title")
	if valid.HasErrors() {
		// validation does not pass
		// print invalid message
		return p, errors.New("hshshshs")
	}
	id, err := o.Insert(&post)
	if err == nil {
		fmt.Println(id)
	}
	return post, nil
}
开发者ID:iamxy,项目名称:beego_react,代码行数:15,代码来源:post.go

示例15: AddCategory

func AddCategory(c Category) (Category, error) {
	o := orm.NewOrm()
	valid := validation.Validation{}
	valid.Required(c.Name, "name")
	if valid.HasErrors() {
		return c, errors.New("Error")
	}
	id, err := o.Insert(&c)
	if err == nil {
		fmt.Println(id)
	} else {
		fmt.Println(err)
	}
	return c, nil
}
开发者ID:lenfree,项目名称:restopos,代码行数:15,代码来源:category.go


注:本文中的github.com/astaxie/beego/validation.Validation.Required方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。