我有一个注册表格,我想用简单的逻辑放在一起,我在服务器端用简单的 ifs 检查了表单验证,例如
if username == "" || < 5 {
session.AddFlash("Username is too short")
session.Save(r, w)
}
这工作正常然后在我做的验证结束时(用于调试目的)
fmt.Println(len(session.Flashes())) which returns 3
然后我检查是否有像这样的任何形式错误
if len(session.Flashes()) != 0 {
// Perform Redirect and show flashes
} else {
// Set proper session variables and log user in
}
那么,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("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)
}
holdtom
相关分类