Hola,
I tried to do radio validation.
I followed this tutorial https://astaxie.gitbooks.io/build-web-application-with-golang/en/04.2.html
Only problem that it gave me an error when i did if and i didn’t understand why.
main.go
func send(w http.ResponseWriter, r *http.Request) {
gender:= []int{1, 2, 3}
msg := &Message{
Name: r.FormValue(“name”),
Surname: r.FormValue(“surname”),
BDay: r.FormValue(“bday”),
Gender: r.FormValue(“gender”),
}
for _, v := range gender{
if v == r.Form.Get(“gender”) {
msg.Errors[“Gender”] = “Please choose your Gender”
}
}
if msg.Validate() == false {
render(w, "templates/index.html", msg)
return
}
// Send message in an email
// Redirect to confirmation page
}
Actually, the for should to be another page message.go, ma i don’t have r *http.Request, i thought to do it into the main.go
message.go
type Message struct {
Name string
Surname string
BDay string
Gender string
City string
Errors map[string]string
}
func (msg *Message) Validate() bool {
msg.Errors = make(map[string]string)
if strings.TrimSpace(msg.Name) == "" {
msg.Errors["Name"] = "Please write your Name"
}
if strings.TrimSpace(msg.Surname) == "" {
msg.Errors["Surname"] = "Please write your Surname"
}
//Gestire la data
if strings.TrimSpace(msg.BDay) == "" {
msg.Errors["BDay"] = "Please select your BDay"
}
//Fare il radio button
if strings.TrimSpace(msg.City) == "" {
msg.Errors["City"] = "Please write your City"
}
return len(msg.Errors) == 0
}