Example #1
0
func (this *baseRouter) validForm(form interface{}, names ...string) (bool, map[string]*validation.ValidationError) {
	// parse request params to form ptr struct
	utils.ParseForm(form, this.Input())

	// Put data back in case users input invalid data for any section.
	name := reflect.ValueOf(form).Elem().Type().Name()
	if len(names) > 0 {
		name = names[0]
	}
	this.Data[name] = form

	errName := name + "Error"

	// check form once
	if this.FormOnceNotMatch() {
		return false, nil
	}

	// Verify basic input.
	valid := validation.Validation{}
	if ok, _ := valid.Valid(form); !ok {
		errs := valid.ErrorMap()
		this.Data[errName] = &valid
		return false, errs
	}
	return true, nil
}
Example #2
0
func Test_Valid(t *testing.T) {
	valid := validation.Validation{}
	user := &models.User{Username: "******", Password: "******"}
	b, err := valid.Valid(user)
	if err != nil {
		// handle error
		t.Log(err.Error())
		t.Error("Valid Error")
	}
	if !b {
		// validation does not pass
		// blabla...
		for _, err := range valid.Errors {
			t.Log(err.Field + "-" + err.String())
			t.Log(err.Key, err.Message)
		}

		t.Log("ppppp")
		for k, v := range valid.ErrorMap() {
			t.Log(k, "=", v)
		}
		t.Error("Valid Error")
	}

	t.Log("Valid not has errors")
}
Example #3
0
func (this *AdminController) AgentOperate() {
	if this.Ctx.Input.Method() == "GET" {
		agentId := this.Ctx.Input.Param(":agentId")
		beego.Info("agentId", agentId)

		agent := &models.Agent{}
		if agentId != "" {
			agent = agent.SelectOneById(agentId)
		}
		this.Data["AgentForm"] = agent
		this.Layout = "layout/main.html"
		this.TplNames = "admin/agent/operate.html"
	} else {
		agent := &models.Agent{}
		if err := this.ParseForm(agent); err != nil {
			this.Data["jons"] = err
			this.ServeJson()
			return
		}

		agent.UserId = this.GetAuthUser().Id
		var valid *validation.Validation
		if agent.Id == "" {
			valid = agent.ValidAgentAdd()
		} else {
			valid = agent.ValidAgentEdit()
		}

		b, _ := valid.Valid(agent)
		if !b {
			this.Data["ErrorMap"] = valid.ErrorMap()
			this.Data["ErrorForm"] = agent
			for k, v := range valid.ErrorMap() {
				beego.Info(k, " Error is ", "-", v.Value, "-", v.Field, "-", v.Tmpl, "-", v.Key, "=", v.Message, "-")
			}
			this.Layout = "layout/main.html"
			this.TplNames = "admin/agent/operate.html"
			return
		}

		var message string
		if agent.Id == "" {
			message = "新增ssh成功"
			if err := agent.InsertOne(); err != nil {
				this.ErrDataBase("新增ssh失败")
				return
			}
		} else {
			message = "更新ssh成功"
			if err := agent.UpdateOne(); err != nil {
				this.ErrDataBase("更新ssh失败")
				return
			}
		}

		this.DoSuccess(message, "/admin/agent/list")

	}
}
Example #4
0
func (this *ServerController) Operate() {
	if this.Ctx.Input.IsGet() {
		serverId := this.Ctx.Input.Param(":serverId")
		beego.Debug("serverId=>", serverId)
		server := &models.Server{}
		if serverId != "" {
			server = server.SelectOneById(serverId)
		}
		this.Data["ServerForm"] = server
		this.Layout = "layout/main.html"
		this.TplNames = "admin/server/operate.html"
	} else {
		server := models.Server{}
		if err := this.ParseForm(&server); err != nil {
			this.ErrInputData("数据错误")
		}

		user := this.GetAuthUser()
		server.UserId = user.Id
		// beego.Debug("server=>",
		// fmt.Sprintf("%+v", server),
		// )
		var valid *validation.Validation
		if server.Id == "" {
			valid = server.ValidServerAdd()
		} else {
			valid = server.ValidServerEdit()
		}

		b, _ := valid.Valid(&server)
		if !b {
			this.Data["ErrorMap"] = valid.ErrorMap()
			this.Data["ErrorForm"] = server
			this.Layout = "layout/main.html"
			this.TplNames = "admin/server/operate.html"
			return
		}

		var message string
		if server.Id == "" {
			message = "新增API配置主题成功"
			if err := server.InsertOne(); err != nil {
				this.ErrDataBase("新增API配置主题失败")
				return
			}
		} else {
			message = "更新API配置主题成功"
			if err := server.UpdateOne(); err != nil {
				this.ErrDataBase("更新API配置主题失败")
				return
			}
		}
		services.GetInstance().CacheService.PutServerContent(server.Id, server.Content)
		this.DoSuccess(message, "/admin/server/index")

	}

}
Example #5
0
// @router /user/operate/?:userId [get,post]
func (this *AdminController) UserOperate() {
	if this.Ctx.Input.Method() == "GET" {
		userId := this.Ctx.Input.Param(":userId")
		beego.Info("userId", userId)

		user := &models.User{}
		if userId != "" {
			user = user.SelectOneById(userId)
		}
		this.Data["UserForm"] = user
		this.Layout = "layout/main.html"
		this.TplNames = "admin/user/operate.html"
		return
	} else {
		user := &models.User{}
		if err := this.ParseForm(user); err != nil {
			this.Data["jons"] = err
			this.ServeJson()
			return
		}
		var valid *validation.Validation
		if user.Id == "" {
			valid = user.ValidUserAdd()
		} else {
			valid = user.ValidUserEdit()
		}

		b, _ := valid.Valid(user)
		if !b {
			this.Data["ErrorMap"] = valid.ErrorMap()
			this.Data["ErrorForm"] = user
			for k, v := range valid.ErrorMap() {
				beego.Info(k, " Error is ", "-", v.Value, "-", v.Field, "-", v.Tmpl, "-", v.Key, "=", v.Message, "-")
			}
			this.Layout = "layout/main.html"
			this.TplNames = "admin/user/operate.html"
			return
		}

		var message string
		if user.Id == "" {
			message = "新增用户成功"
			if err := user.InsertOne(); err != nil {
				this.ErrDataBase("新增用户失败")
				return
			}
		} else {
			message = "更新用户成功"
			if err := user.UpdateOne(); err != nil {
				this.ErrDataBase("更新用户失败")
				return
			}
		}

		this.DoSuccess(message, "/admin/user/list")
	}

}
Example #6
0
func (this *AdminController) CmdOperate() {
	if this.Ctx.Input.Method() == "GET" {
		cmdId := this.Ctx.Input.Param(":cmdId")
		agentId := this.Ctx.Input.Param(":agentId")
		beego.Info("cmdId", cmdId)

		cmd := &models.Cmd{}
		if cmdId != "" {
			cmd = cmd.SelectOneById(cmdId)
		}
		cmd.AgentId = agentId
		this.Data["CmdForm"] = cmd
		this.Layout = "layout/main.html"
		this.TplNames = "admin/cmd/operate.html"
	} else {
		cmd := &models.Cmd{}
		if err := this.ParseForm(cmd); err != nil {
			this.Data["jons"] = err
			this.ServeJson()
			return
		}
		var valid *validation.Validation
		if cmd.Id == "" {
			valid = cmd.ValidCmdAdd()
		} else {
			valid = cmd.ValidCmdEdit()
		}

		b, _ := valid.Valid(cmd)
		if !b {
			this.Data["ErrorMap"] = valid.ErrorMap()
			this.Data["ErrorForm"] = cmd
			this.Layout = "layout/main.html"
			this.TplNames = "admin/cmd/operate.html"
			return
		}

		var message string
		if cmd.Id == "" {
			message = "新增cmd成功"
			if err := cmd.InsertOne(); err != nil {
				this.ErrDataBase("新增cmd失败")
				return
			}
		} else {
			message = "更新cmd成功"
			if err := cmd.UpdateOne(); err != nil {
				this.ErrDataBase("更新ssh失败")
				return
			}
		}

		this.DoSuccess(message, "/admin/agent/list")

	}
}
Example #7
0
func Test_ValidUserAdd(t *testing.T) {
	user := models.User{Username: "******", Password: "******"}
	var valid *validation.Validation
	valid = user.ValidUserAdd()
	b, _ := valid.Valid(user)
	if !b {
		for k, v := range valid.ErrorMap() {
			beego.Info(k, " Error is ", "-", v.Value, "-", v.Field, "-", v.Tmpl, "-", v.Key, "=")
		}
	}

}