Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model中的表单数据验证不起作用的问题 #48

Open
chenwaichung opened this issue Jun 26, 2016 · 0 comments
Open

Model中的表单数据验证不起作用的问题 #48

chenwaichung opened this issue Jun 26, 2016 · 0 comments

Comments

@chenwaichung
Copy link

列如用户表单中,用户名称和密码最小是需要6位的
Username string orm:"unique;size(32)" form:"Username" valid:"Required;MaxSize(20);MinSize(6)"
Password string orm:"size(32)" form:"Password" valid:"Required;MaxSize(20);MinSize(6)"

我测试时表单数据验证不起作用,比如输入jack,密码两次输入都不一样就可以新建用户成功了。
然后阅读源码修改UserModel.go的checkUser函数中 b, _ := valid.Valid(&u) 为 b, _ := valid.Valid(u) 或者b, _ := valid.Valid(*u) ,即可以正常表单数据验证,修改如下
//验证用户信息
func checkUser(u *User) (err error) {
valid := validation.Validation{}
b, _ := valid.Valid(u)//此处将取地址符合去掉了,表单数据验证就有效
if !b {
for _, err := range valid.Errors {
log.Println(err.Key, err.Message)
return errors.New(err.Message)
}
}
return nil
}

以我个人理解应该是传入是指针,指针已经指向了地址,所以无需再取地址了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant