binding icon indicating copy to clipboard operation
binding copied to clipboard

Custom validation on the documentation can not be compiled (undefined "rule" error).

Open toni-moreno opened this issue 8 years ago • 2 comments

The custom validation on the example is not working.

func init() {
	// Min(1) set de minimun integer value
	binding.AddRule(&binding.Rule{
		IsMatch: func(rule string) bool {
			return strings.HasPrefix(rule, "Min(")
		},
		IsValid: func(errs binding.Errors, name string, v interface{}) (bool, binding.Errors) {
			num, ok := v.(int)
			if !ok {
				return false, errs
			}
			min, _ := strconv.Atoi(rule[4 : len(rule)-1])
			if num < min {
				errs.Add([]string{name}, "MinimumValue", "Value is too small")
				return false, errs
			}
			return true, errs
		},
	})

}

On compile we have this error.

pkg/webui/extravalidations.go:22: undefined: rule

There is no "rule" variable defined inside IsValid function. But I need build custom rules similars to the Min(10) example . How can I access to the rule definition in the IsValid function?

toni-moreno avatar Apr 22 '17 05:04 toni-moreno

Hi, please update this package and check out new docs for custom validation rule: https://go-macaron.com/docs/middlewares/binding#custom-validation-rules

unknwon avatar May 21 '17 09:05 unknwon

Hi @Unknwon .

I will do some test next days and I will report you about.

Thank you very much

toni-moreno avatar May 24 '17 12:05 toni-moreno