Adding ability to customize form guard field name
M form_guard/forms.py +23 -17
@@ 5,21 5,27 @@ from django.core.exceptions import Permi
 from .settings import FORM_GUARD_MIN_VALUE
 
 
-class FormGuardForm(forms.Form):
-    antispam_ = forms.CharField(
-        widget=forms.HiddenInput(),
-        initial='default',
-        attrs={'id': 'id_antispam_'})
-    )
+def clean_func(self):
+    antispam = self.cleaned_data.get('antispam')
+    if settings.DEBUG:
+        return antispam
+    try:
+        antispam = int(antispam)
+    except ValueError:
+        raise PermissionDenied
+    if antispam < FORM_GUARD_MIN_VALUE:
+        raise PermissionDenied
+    return antispam
 
-    def clean_antispam_(self):
-        antispam = self.cleaned_data.get('antispam')
-        if settings.DEBUG:
-            return antispam
-        try:
-            antispam = int(antispam)
-        except ValueError:
-            raise PermissionDenied
-        if antispam < FORM_GUARD_MIN_VALUE:
-            raise PermissionDenied
-        return antispam
+
+class FormGuardForm(forms.Form):
+    FORM_GUARD_FIELD_NAME = 'antispam'
+
+    def __init__(self, *args, **kwargs):
+        super(FormGuardForm, self).__init__(*args, **kwargs)
+        self.fields[FORM_GUARD_FIELD_NAME] = forms.CharField(
+            widget=forms.HiddenInput(),
+            initial='default',
+            attrs={'id': 'id_{0}'.format(FORM_GUARD_FIELD_NAME)})
+        )
+        setattr(self, 'clean_{0}'.format(FORM_GUARD_FIELD_NAME), clean_func)

          
M form_guard/templates/form_guard/js_snippet.html +2 -2
@@ 1,7 1,7 @@ 
 <script type="text/javascript">
   var antiSpam = function() {
-      if (document.getElementById("id_antispam")) {
-          a = document.getElementById("id_antispam");
+      if (document.getElementById("{{ FORM_GUARD_FIELD_ID }}")) {
+          a = document.getElementById("{{ FORM_GUARD_FIELD_ID }}");
           if (isNaN(a.value) == true) {
               a.value = 0;
           } else {

          
A => form_guard/templatetags/__init__.py +0 -0

        
A => form_guard/templatetags/form_guard_tags.py +11 -0
@@ 0,0 1,11 @@ 
+# -*- coding: utf-8 -*-
+from django import template
+
+from .forms import FormGuardForm
+
+register = template.Library()
+
+
+@register.inclusion_tag('form_guard/js_snippet.html')
+def form_guard_js(context, form):
+    return {'FORM_GUARD_FIELD_ID': form.fields[form.FORM_GUARD_FIELD_NAME].id}