3 files changed, 59 insertions(+), 0 deletions(-)

A => formguard.go
A => go.mod
A => script.js
A => formguard.go +55 -0
@@ 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)
+}

          
A => go.mod +3 -0
@@ 0,0 1,3 @@ 
+module example.com/gobwebs-formguard
+
+go 1.19

          
A => script.js +1 -0
@@ 0,0 1,1 @@ 
+console.log("This is a test");