Comments (9)
func (form *VariantForm) Error(ctx *macaron.Context, errs binding.Errors) {}
不是很懂你这个方法是怎么来的。。
from binding.
你好,是这样的
比如我要自定义form的验证方法,可以这样写:
func (form *MyForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
///
}
也可以这样写
func (form MyForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
///
}
而自定义错误处理,只能写成这样:
func (cf ContactForm) Error(ctx *macaron.Context, errs binding.Errors) {
// 自定义错误处理过程
}
写成下面这样不起作用
func (cf *ContactForm) Error(ctx *macaron.Context, errs binding.Errors) {
// �自定义错误处理过程不起作用,
}
from binding.
能否发一下你绑定某个 form(不起作用的) 是怎么写的?
from binding.
// 商品型号
type VariantForm struct {
OptionValues []int
Price float32 `binding:"Required"`
Sku string
}
// 验证选项值需要全部大于0
func (form *VariantForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
if len(errs) > 0 {
return errs
}
for _, v := range form.OptionValues {
if v == 0 {
errs.Add([]string{"OptionValues"}, "RequiredError", "Required")
break
}
}
return errs
}
func (form *VariantForm) Error(ctx *macaron.Context, errs binding.Errors) {
if len(errs) > 0 {
flash := ctx.GetVal(reflect.TypeOf(&session.Flash{})).Interface().(*session.Flash)
errMsg := fmt.Sprintf("%s: %s", strings.Join(errs[0].FieldNames, ","), errs[0].Message)
flash.Error(errMsg)
ctx.Redirect(fmt.Sprintf("/variants/%d/edit", 30))
}
}
自定义错误处理写成下面可以运行:
func (form VariantForm) Error(ctx *macaron.Context, errs binding.Errors) {
if len(errs) > 0 {
flash := ctx.GetVal(reflect.TypeOf(&session.Flash{})).Interface().(*session.Flash)
errMsg := fmt.Sprintf("%s: %s", strings.Join(errs[0].FieldNames, ","), errs[0].Message)
flash.Error(errMsg)
ctx.Redirect(fmt.Sprintf("/variants/%d/edit", 30))
}
}
from binding.
额。。其实我想说的是,注册路由部分的代码。。
from binding.
好的,代码是这样的:
type VariantsController struct {
*macaron.Context
}
func MapVariants(ctx *macaron.Context) {
ctx.Map(&VariantsController{ctx})
}
m.Group("/variants", func() {
m.Get("/:id/edit", (*VariantsController).Edit)
m.Put("/:id", binding.Bind(VariantForm{}), (*VariantsController).Update)
}, MapVariants)
from binding.
额。。看起来没问题。。我看了代码,判断 Validate 和 Error 的用法是一样的。。暂时不知道为什么。。0 0
from binding.
似乎是Bind 方法里面没有判断指针类型的原因
binding.go 104行
func Bind(obj interface{}, ifacePtr ...interface{}) macaron.Handler {
return func(ctx *macaron.Context) {
bind(ctx, obj, ifacePtr...)
if handler, ok := obj.(ErrorHandler); ok {
ctx.Invoke(handler.Error)
} else {
ctx.Invoke(errorHandler)
}
}
}
第210行
func Validate(obj interface{}) macaron.Handler {
return func(ctx *macaron.Context) {
var errors Errors
v := reflect.ValueOf(obj)
k := v.Kind()
if k == reflect.Interface || k == reflect.Ptr {
v = v.Elem()
k = v.Kind()
}
if k == reflect.Slice || k == reflect.Array {
for i := 0; i < v.Len(); i++ {
e := v.Index(i).Interface()
errors = validateStruct(errors, e)
if validator, ok := e.(Validator); ok {
errors = validator.Validate(ctx, errors)
}
}
} else {
errors = validateStruct(errors, obj)
if validator, ok := obj.(Validator); ok {
errors = validator.Validate(ctx, errors)
}
}
ctx.Map(errors)
}
}
from binding.
噢。。那这个目前我还没想到什么好的办法,因为 Bind 是禁止你传指针的,否则会出现大量数据竞争破坏内存出现奇怪的错误
from binding.
Related Issues (20)
- Unicode support
- Cannot apply validation rule to pointer field *string
- Related resource validation? HOT 1
- parsing errors from setWithProperType never get bubbled up
- 无法解析数组参数 HOT 10
- Error on upload Files - no data sent to the handler. HOT 7
- 建议增加设置,可自定义 binding 标识 HOT 5
- Regex Validation examples? HOT 3
- Custom validation on the documentation can not be compiled (undefined "rule" error). HOT 2
- Error In Validation "In()" also implies Required HOT 1
- [Feature Request] Enable macaron binding Validation outside a request context. HOT 3
- Multiple sub-forms from html not json ? HOT 2
- Security checks for Json() HOT 3
- 建议可以自定义默认的errorHandler HOT 1
- Pls upd mod vendor as in main macaron package HOT 2
- case-insensitive import collision: "github.com/unknwon/com" and "github.com/Unknwon/com" HOT 1
- GET requests with content-type header set result in errors HOT 3
- Add unit tests for `binding.URL`
- 纯ip格式可以通过binding.Url校验
- Called inject.InterfaceOf with a value that is not a pointer to an interface. (*MyInterface)(nil)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from binding.