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

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

Open
toni-moreno opened this issue Apr 22, 2017 · 2 comments

Comments

@toni-moreno
Copy link

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 toni-moreno changed the title Custom validation on the documentation can not be complited (undefined "rule" error). Custom validation on the documentation can not be compiled (undefined "rule" error). Apr 22, 2017
@unknwon unknwon added the bug label May 21, 2017
@unknwon unknwon self-assigned this May 21, 2017
unknwon added a commit that referenced this issue May 21, 2017
@unknwon
Copy link
Contributor

unknwon commented May 21, 2017

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

@toni-moreno
Copy link
Author

Hi @unknwon .

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

Thank you very much

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

No branches or pull requests

2 participants