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

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

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:

package models

import (
	"../config"
	"../util"
)

type Form struct {
	Name    string
	Email   string
	Phone   string
	Message string
	Thanks  string
	ErrorHandler
}

func (this *Form) Validate() bool {
	this.Errors = make(map[string]string)

	matched := util.MatchRegexp(".+@.+\\..+", this.Email)

	if !util.IsEmpty(this.Email) {
		if matched == false {
			this.Errors["Email"] = config.EMAIL_INVALID
		}
	} else {
		this.Errors["Email"] = config.EMAIL_EMPTY
	}

	if util.IsEmpty(this.Name) {
		this.Errors["Name"] = config.NAME_EMPTY
	}

	if util.IsEmpty(this.Phone) {
		this.Errors["Phone"] = config.PHONE_EMPTY
	}

	if util.IsEmpty(this.Message) {
		this.Errors["Message"] = config.MESSAGE_EMPTY
	}

	validated := len(this.Errors) == 0

	if validated {
		this.Thanks = config.THANK_YOU
	}

	return validated
}

errorhandler.go:

package models

type ErrorHandler struct {
	Errors map[string]string
}

func (this *ErrorHandler) HandleErr(err string) {
	this.Errors = make(map[string]string)
	this.Errors["Error"] = err
}

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

form := &models.Form{
	Name:    r.FormValue("name"),
	Email:   r.FormValue("email"),
	Phone:   r.FormValue("phone"),
	Message: r.FormValue("message")}

if form.Validate() {
	// 这里永远不会运行,因为'form.Validate()'总是false
}

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

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

任何帮助将不胜感激!

英文:

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:

package models

import (
	"../config"
	"../util"
)

type Form struct {
	Name    string
	Email   string
	Phone   string
	Message string
	Thanks  string
	ErrorHandler
}

func (this *Form) Validate() bool {
	this.Errors = make(map[string]string)

	matched := util.MatchRegexp(".+@.+\\..+", this.Email)

	if !util.IsEmpty(this.Email) {
		if matched == false {
			this.Errors["Email"] = config.EMAIL_INVALID
		}
	} else {
		this.Errors["Email"] = config.EMAIL_EMPTY
	}

	if util.IsEmpty(this.Name) {
		this.Errors["Name"] = config.NAME_EMPTY
	}

	if util.IsEmpty(this.Phone) {
		this.Errors["Phone"] = config.PHONE_EMPTY
	}

	if util.IsEmpty(this.Message) {
		this.Errors["Message"] = config.MESSAGE_EMPTY
	}

	validated := len(this.Errors) == 0

	if validated {
		this.Thanks = config.THANK_YOU
	}

	return validated
}

errorhandler.go:

package models

type ErrorHandler struct {
	Errors map[string]string
}

func (this *ErrorHandler) HandleErr(err string) {
	this.Errors = make(map[string]string)
	this.Errors["Error"] = err
}

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

form := &models.Form{
	Name:    r.FormValue("name"),
	Email:   r.FormValue("email"),
	Phone:   r.FormValue("phone"),
	Message: r.FormValue("message")}

if form.Validate() {
	// This never runs because 'form.Validate()' is always false
}

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

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

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:

确定