Coder Social home page Coder Social logo

Comments (9)

unknwon avatar unknwon commented on May 20, 2024

func (form *VariantForm) Error(ctx *macaron.Context, errs binding.Errors) {}

不是很懂你这个方法是怎么来的。。

from binding.

cgyy avatar cgyy commented on May 20, 2024

你好,是这样的
比如我要自定义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.

unknwon avatar unknwon commented on May 20, 2024

能否发一下你绑定某个 form(不起作用的) 是怎么写的?

from binding.

cgyy avatar cgyy commented on May 20, 2024
// 商品型号
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.

unknwon avatar unknwon commented on May 20, 2024

额。。其实我想说的是,注册路由部分的代码。。

from binding.

cgyy avatar cgyy commented on May 20, 2024

好的,代码是这样的:

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.

unknwon avatar unknwon commented on May 20, 2024

额。。看起来没问题。。我看了代码,判断 Validate 和 Error 的用法是一样的。。暂时不知道为什么。。0 0

from binding.

cgyy avatar cgyy commented on May 20, 2024

似乎是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.

unknwon avatar unknwon commented on May 20, 2024

噢。。那这个目前我还没想到什么好的办法,因为 Bind 是禁止你传指针的,否则会出现大量数据竞争破坏内存出现奇怪的错误

from binding.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.