Skip to content

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

Description

@toni-moreno

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?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions