@@ 1,7 1,7 @@
package formguard
import (
- "errors"
+ "fmt"
"html/template"
"strings"
@@ 32,7 32,7 @@ func (f *Form) Validate(c echo.Context)
}
if f.AntiSpam < FormGuardMinValue {
- return errors.New("This operation is forbidden")
+ return validate.InputErrors{"_global_": []string{"This operation is forbbiden."}}
}
return nil
@@ 41,9 41,30 @@ func (f *Form) Validate(c echo.Context)
// ScriptJS ...
func ScriptJS(ids ...string) template.HTML {
script := `<script>
- let ids = '<idsList>';
+ var update_field_value = function(field_id) {
+ if (document.getElementById(field_id)) {
+ a = document.getElementById(field_id);
+ if (isNaN(a.value) == true) {
+ a.value = 0;
+ } else {
+ a.value = parseInt(a.value) + 1;
+ }
+ }
+ }
+ let field_ids = [<idsList>];
+ var anti_spam = function() {
+ for (var i=0, l=field_ids.length; i<l; i++) {
+ update_field_value(field_ids[i]);
+ }
+ setTimeout("anti_spam()", 1000);
+ }
+ anti_spam();
</script>`
- script = strings.Replace(script, "<idsList>", strings.Join(ids, ","), 1)
+ var idsList string
+ for _, id := range ids {
+ idsList += fmt.Sprintf("'%s',", id)
+ }
+ script = strings.Replace(script, "<idsList>", idsList, 1)
return template.HTML(script)
}