英文:
Gorilla web toolikit: len(session.Flashes()) is 0
问题
我有一个正在组合的注册表单,使用简单的逻辑在服务器端进行表单验证,例如:
if username == "" || len(username) < 5 {
session.AddFlash("Username is too short")
session.Save(r, w)
}
这个逻辑运行良好,然后在验证的最后,我进行以下操作(用于调试):
fmt.Println(len(session.Flashes())) // 返回 3
然后我检查是否有任何表单错误,如下所示:
if len(session.Flashes()) != 0 {
// 执行重定向并显示错误信息
} else {
// 设置正确的会话变量并登录用户
}
所以,如果 3 > 0
,为什么触发的是 else
语句而不是 if
语句的第一部分呢?我不确定为什么会出现这种情况。如果需要更多信息,请告诉我。以下是实际的代码片段:
if username == "" || len(username) < 4 {
session.AddFlash("Username is too short")
session.Save(r, w)
}
if email == "" || len(email) < 5 {
session.AddFlash("Email is too short")
session.Save(r, w)
}
if firstname == "" || len(firstname) < 3 {
session.AddFlash("Firstname is too short")
session.Save(r, w)
}
if lastname == "" || len(lastname) < 3 {
session.AddFlash("Lastname is too short")
session.Save(r, w)
}
fmt.Println(len(session.Flashes()) > 0) // 返回 true
if len(session.Flashes()) != 0 {
fmt.Println("为什么我也在这里")
type Page struct {
Title string
Username interface{}
Errors []interface{}
}
session, _ := common.GetSession(r)
data := &Page{"Register", session.Values["username"], session.Flashes()}
session.Save(r, w)
tmpl, err := template.ParseFiles("views/register/register.html")
if err != nil {
http.Error(w, "加载页面失败。", 500)
}
tmpl.ExecuteTemplate(w, "register", data)
} else {
fmt.Println("为什么我在这里")
_, err := db.Query("// 执行数据库查询")
if err != nil {
http.Error(w, "抱歉,我们在将您的帐户保存到数据库时遇到问题,请稍后再试。", 500)
}
session.Values["username"] = r.FormValue("username")
session.Values["authenticated"] = true
session.Save(r, w)
http.Redirect(w, r, "/", 303)
}
英文:
I have a register form I'm trying to put together, with simple logic I have form validation checked on the server side with simple ifs e.g.
if username == "" || < 5 {
session.AddFlash("Username is too short")
session.Save(r, w)
}
That works fine then at the end of the validation I do (for debugging purposes)
fmt.Println(len(session.Flashes())) which returns 3
Then I check to see if there were any form errors like so
if len(session.Flashes()) != 0 {
// Perform Redirect and show flashes
} else {
// Set proper session variables and log user in
}
So if 3 > 0
why is the else
statement being triggered and not the first part of the if statement? I'm just unsure why this is happening the way it is. Thanks if you need anymore information let me know, actual code snippet:
if username == "" || len(username) < 4 {
session.AddFlash("Username is too short")
session.Save(r, w)
}
if email == "" || len(email) < 5 {
session.AddFlash("Email is too short")
session.Save(r, w)
}
if firstname == "" || len(firstname) < 3 {
session.AddFlash("Firstname is too short")
session.Save(r, w)
}
if lastname == "" || len(lastname) < 3 {
session.AddFlash("Lastname is too short")
session.Save(r, w)
}
fmt.Println(len(session.Flashes()) > 0) // true
if len(session.Flashes()) != 0 {
fmt.Println("Why am I here also")
type Page struct {
Title string
Username interface{}
Errors []interface{}
}
session, _ := common.GetSession(r)
data := &Page{"Register", session.Values["username"], session.Flashes()}
session.Save(r, w)
tmpl, err := template.ParseFiles("views/register/register.html")
if err != nil {
http.Error(w, "Failed to load page.", 500)
}
tmpl.ExecuteTemplate(w, "register", data)
} else {
fmt.Println("Why am I here")
_, err := db.Query("// Perform DB Query")
if err != nil {
http.Error(w, "Sorry we had trouble saving your account to the database, try again in a bit.", 500)
}
session.Values["username"] = r.FormValue("username")
session.Values["authenticated"] = true
session.Save(r, w)
http.Redirect(w, r, "/", 303)
}
答案1
得分: 1
这是要翻译的内容:
这段代码的文档不是很完善,但显然Flashes
函数会从会话中移除闪存并返回它们:
func (s *Session) Flashes(vars ...string) []interface{} {
var flashes []interface{}
key := flashesKey
if len(vars) > 0 {
key = vars[0]
}
if v, ok := s.Values[key]; ok {
// 删除闪存并返回它
delete(s.Values, key)
flashes = v.([]interface{})
}
return flashes
}
源代码在这里。
这里的解决方案是使用一个单独的变量来保存验证状态:
valid := true
if username == "" || len(username) < 4 {
valid = false
session.AddFlash("用户名太短")
session.Save(r, w)
}
// ...
if !valid {
// ...
} else {
// ...
}
编辑: 另一种获取闪存而不删除它们的方法是直接从Values
中获取:
flashes := session.Values["_flash"].([]interface{})
英文:
It's not very well documented, but apparently Flashes
removes flashes from the session and returns them:
func (s *Session) Flashes(vars ...string) []interface{} {
var flashes []interface{}
key := flashesKey
if len(vars) > 0 {
key = vars[0]
}
if v, ok := s.Values[key]; ok {
// Drop the flashes and return it.
delete(s.Values, key)
flashes = v.([]interface{})
}
return flashes
}
Source code here.
The solution here is to use a separate variable to save the validation state:
valid := true
if username == "" || len(username) < 4 {
valid = false
session.AddFlash("Username is too short")
session.Save(r, w)
}
// ...
if !valid {
// ...
} else {
// ...
}
EDIT: Another way to get flashes without deleting them is to get them from Values
directly:
flashes := session.Values["_flash"].([]interface{})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论