为什么我的结构体方法始终返回false?

huangapple go评论111阅读模式
英文:

Why is my struct method always returning false?

问题

我正在尝试在一个返回布尔值的方法中对我的表单结构进行验证,但是即使应该返回true,我仍然得到false。

如果你看一下Validate方法的最后部分,你会看到我写了validated := len(this.Errors) == 0,根据Errors映射是否有项目,"validated"应该是true或false,然后我返回validated。

当我准确填写表单时,不应该有错误,但是我仍然得到false,而不是true。

有人能解释一下吗?Go不是这样工作的吗?

form.go:

  1. package models
  2. import (
  3. "../config"
  4. "../util"
  5. )
  6. type Form struct {
  7. Name string
  8. Email string
  9. Phone string
  10. Message string
  11. Thanks string
  12. ErrorHandler
  13. }
  14. func (this *Form) Validate() bool {
  15. this.Errors = make(map[string]string)
  16. matched := util.MatchRegexp(".+@.+\\..+", this.Email)
  17. if !util.IsEmpty(this.Email) {
  18. if matched == false {
  19. this.Errors["Email"] = config.EMAIL_INVALID
  20. }
  21. } else {
  22. this.Errors["Email"] = config.EMAIL_EMPTY
  23. }
  24. if util.IsEmpty(this.Name) {
  25. this.Errors["Name"] = config.NAME_EMPTY
  26. }
  27. if util.IsEmpty(this.Phone) {
  28. this.Errors["Phone"] = config.PHONE_EMPTY
  29. }
  30. if util.IsEmpty(this.Message) {
  31. this.Errors["Message"] = config.MESSAGE_EMPTY
  32. }
  33. validated := len(this.Errors) == 0
  34. if validated {
  35. this.Thanks = config.THANK_YOU
  36. }
  37. return validated
  38. }

errorhandler.go:

  1. package models
  2. type ErrorHandler struct {
  3. Errors map[string]string
  4. }
  5. func (this *ErrorHandler) HandleErr(err string) {
  6. this.Errors = make(map[string]string)
  7. this.Errors["Error"] = err
  8. }

这是我尝试调用Validate方法的地方 - 在我的控制器中的一个函数中:

  1. form := &models.Form{
  2. Name: r.FormValue("name"),
  3. Email: r.FormValue("email"),
  4. Phone: r.FormValue("phone"),
  5. Message: r.FormValue("message")}
  6. if form.Validate() {
  7. // 这里永远不会运行,因为'form.Validate()'总是false
  8. }

我不认为util.IsEmpty()是问题所在...它只是检查字符串是否为空:

  1. func IsEmpty(str string) bool {
  2. return strings.TrimSpace(str) == ""
  3. }

任何帮助将不胜感激!

英文:

I'm trying to do validation on my form struct in a method that returns a bool, but I keep getting false even when it should be returning true..

If you look towards the end of the Validate method, you'll see I write validated := len(this.Errors) == 0 which should be making "validated" either true or false based on whether the Errors map has items or not, and then I return validated.

When I fill out my form accurately, there should be no errors yet I still get false when I should be getting true.

Can someone explain? Is this not how Go works?

form.go:

  1. package models
  2. import (
  3. "../config"
  4. "../util"
  5. )
  6. type Form struct {
  7. Name string
  8. Email string
  9. Phone string
  10. Message string
  11. Thanks string
  12. ErrorHandler
  13. }
  14. func (this *Form) Validate() bool {
  15. this.Errors = make(map[string]string)
  16. matched := util.MatchRegexp(".+@.+\\..+", this.Email)
  17. if !util.IsEmpty(this.Email) {
  18. if matched == false {
  19. this.Errors["Email"] = config.EMAIL_INVALID
  20. }
  21. } else {
  22. this.Errors["Email"] = config.EMAIL_EMPTY
  23. }
  24. if util.IsEmpty(this.Name) {
  25. this.Errors["Name"] = config.NAME_EMPTY
  26. }
  27. if util.IsEmpty(this.Phone) {
  28. this.Errors["Phone"] = config.PHONE_EMPTY
  29. }
  30. if util.IsEmpty(this.Message) {
  31. this.Errors["Message"] = config.MESSAGE_EMPTY
  32. }
  33. validated := len(this.Errors) == 0
  34. if validated {
  35. this.Thanks = config.THANK_YOU
  36. }
  37. return validated
  38. }

errorhandler.go:

  1. package models
  2. type ErrorHandler struct {
  3. Errors map[string]string
  4. }
  5. func (this *ErrorHandler) HandleErr(err string) {
  6. this.Errors = make(map[string]string)
  7. this.Errors["Error"] = err
  8. }

And this is where I try to call the Validate method -- in a function in my controller:

  1. form := &models.Form{
  2. Name: r.FormValue("name"),
  3. Email: r.FormValue("email"),
  4. Phone: r.FormValue("phone"),
  5. Message: r.FormValue("message")}
  6. if form.Validate() {
  7. // This never runs because 'form.Validate()' is always false
  8. }

I don't think the util.IsEmpty() is the culprit here.. just checks if the string is empty:

  1. func IsEmpty(str string) bool {
  2. return strings.TrimSpace(str) == ""
  3. }

Any help would be appreciated!

答案1

得分: 1

最好使用类似以下的日志语句来调试这种问题:

log.Printf("form: %v", form)

在调用validate之前,这样可以清楚地了解输入数据的样式。

问候,Philip

英文:

It's best to debug this kind of problem with a log statement like:

log.Printf("form: %v", form)

before calling validate, so it's clear what the input data looks like.

Greetings, Philip

huangapple
  • 本文由 发表于 2016年1月24日 02:35:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/34967215.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定