# HG changeset patch # User Yader Velasquez # Date 1671469767 21600 # Mon Dec 19 11:09:27 2022 -0600 # Node ID b84777c9a94db0c01550a877de42773e7fe4e159 # Parent fed5da1379e3909e4ae8e6da4037715707394fd5 Include MinValue as form attr and use name instead of id in template tags diff --git a/formguard.go b/formguard.go --- a/formguard.go +++ b/formguard.go @@ -3,7 +3,6 @@ import ( "fmt" "html/template" - "strings" "github.com/labstack/echo/v4" "hg.code.netlandish.com/~netlandish/gobwebs/validate" @@ -11,17 +10,15 @@ // Form ... type Form struct { - AntiSpam int `form:"antispam"` + AntiSpamField int + MinValue int } -// XXX hardcoded for now -var FormGuardMinValue int = 5 - // Validate ... -func (f *Form) Validate(c echo.Context) error { +func (f *Form) Validate(c echo.Context, fieldName string) error { errs := echo.FormFieldBinder(c). FailFast(false). - Int("antispam", &f.AntiSpam). + Int(fieldName, &f.AntiSpamField). BindErrors() if errs != nil { return validate.GetInputErrors(errs) @@ -31,7 +28,11 @@ return err } - if f.AntiSpam < FormGuardMinValue { + if f.MinValue == 0 { + f.MinValue = 5 + } + + if f.AntiSpamField < f.MinValue { return validate.InputErrors{"_global_": []string{"This operation is forbbiden."}} } @@ -39,8 +40,12 @@ } // ScriptJS ... -func ScriptJS(ids ...string) template.HTML { - script := `` - var idsList string - for _, id := range ids { - idsList += fmt.Sprintf("'%s',", id) - } - script = strings.Replace(script, "", idsList, 1) + `, nameList) return template.HTML(script) } // InputHTML ... -func InputHTML(id string) template.HTML { - input := "" - input = strings.Replace(input, "", id, 1) +func InputHTML(name string) template.HTML { + input := fmt.Sprintf("", name, name) return template.HTML(input) }