@@ 0,0 1,55 @@
+package formguard
+
+import (
+ "errors"
+ "html/template"
+ "strings"
+
+ "github.com/labstack/echo/v4"
+ "hg.code.netlandish.com/~netlandish/gobwebs/validate"
+)
+
+// Form ...
+type Form struct {
+ AntiSpam int `form:"antispam"`
+}
+
+// XXX hardcoded for now
+var FormGuardMinValue int = 5
+
+// Validate ...
+func (f *Form) Validate(c echo.Context) error {
+ errs := echo.FormFieldBinder(c).
+ FailFast(false).
+ Int("antispam", &f.AntiSpam).
+ BindErrors()
+ if errs != nil {
+ return validate.GetInputErrors(errs)
+ }
+
+ if err := c.Validate(f); err != nil {
+ return err
+ }
+
+ if f.AntiSpam < FormGuardMinValue {
+ return errors.New("This operation is forbidden")
+ }
+
+ return nil
+}
+
+// ScriptJS ...
+func ScriptJS(ids ...string) template.HTML {
+ script := `<script>
+ let ids = '<idsList>';
+ </script>`
+ script = strings.Replace(script, "<idsList>", strings.Join(ids, ","), 1)
+ return template.HTML(script)
+}
+
+// InputHTML ...
+func InputHTML(id string) template.HTML {
+ input := "<input type='hidden' name='antispan' value='0' id='<elementID>'/>"
+ input = strings.Replace(input, "<elementID>", id, 1)
+ return template.HTML(input)
+}