# HG changeset patch # User Yader Velasquez # Date 1671479452 21600 # Mon Dec 19 13:50:52 2022 -0600 # Node ID f924a8f357d77ea4532b6fe1df0790b8871f1241 # Parent 0000000000000000000000000000000000000000 Init diff --git a/formguard.go b/formguard.go new file mode 100644 --- /dev/null +++ b/formguard.go @@ -0,0 +1,80 @@ +package formguard + +import ( + "fmt" + "html/template" + + "github.com/labstack/echo/v4" + "hg.code.netlandish.com/~netlandish/gobwebs/validate" +) + +// Form ... +type Form struct { + AntiSpamField int + MinValue int + FieldName string +} + +// Validate ... +func (f *Form) Validate(c echo.Context) error { + if f.FieldName == "" { + f.FieldName = "antispam" + } + + errs := echo.FormFieldBinder(c). + FailFast(false). + Int(f.FieldName, &f.AntiSpamField). + BindErrors() + if errs != nil { + return validate.GetInputErrors(errs) + } + + if err := c.Validate(f); err != nil { + return err + } + + if f.MinValue == 0 { + f.MinValue = 5 + } + + if f.AntiSpamField < f.MinValue { + return validate.InputErrors{"_global_": []string{"This operation is forbbiden."}} + } + + return nil +} + +// ScriptJS ... +func ScriptJS(names ...string) template.HTML { + var nameList string + for _, name := range names { + nameList += fmt.Sprintf("'id_%s',", name) + } + script := fmt.Sprintf(``, nameList) + return template.HTML(script) +} + +// InputHTML ... +func InputHTML(name string) template.HTML { + input := fmt.Sprintf("", name, name) + return template.HTML(input) +} diff --git a/go.mod b/go.mod new file mode 100644 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module hg.code.netlandish.com/~netlandish/gobwebs-formguard + +go 1.19