Repository: zostera/django-bootstrap3

Issue:
required_css_class argument in {% bootstrap_form form ... %}
Hi!

I'm trying to set required_css_class for a specific form rendering without using it like a global setting in BOOTSTRAP3.
So, when I read http://django-bootstrap3.readthedocs.org/en/latest/templatetags.html#bootstrap-form ("See bootstrap_field for other arguments") I understood that by doing this:
`{% bootstrap_form myform required_css_class='required' %}`
I get what I wanted... but it didn't work... :(

Is there a problem in bootstrap_form template tags? or am I doing something wrong?

I'm using django-bootstrap3 6.2.2

Thank you very much in advance for any help



Discussion:
I’ve the same problem with the 'bound_css_class', passed as argument of the bootstrap_form templatetag. Those arguments are not passed to the field renderer. I’m about to send a pull request to fix this.


Resolving patch:
diff --git a/bootstrap3/renderers.py b/bootstrap3/renderers.py
--- a/bootstrap3/renderers.py
+++ b/bootstrap3/renderers.py
@@ -151,6 +151,9 @@ def __init__(self, form, *args, **kwargs):
         # Handle form.empty_permitted
         if self.form.empty_permitted:
             self.set_required = False
+        self.error_css_class = kwargs.get('error_css_class', None)
+        self.required_css_class = kwargs.get('required_css_class', None)
+        self.bound_css_class = kwargs.get('bound_css_class', None)
 
     def render_fields(self):
         rendered_fields = []
@@ -169,6 +172,9 @@ def render_fields(self):
                 size=self.size,
                 horizontal_label_class=self.horizontal_label_class,
                 horizontal_field_class=self.horizontal_field_class,
+                error_css_class=self.error_css_class,
+                required_css_class=self.required_css_class,
+                bound_css_class=self.bound_css_class,
             ))
         return '\n'.join(rendered_fields)
 
@@ -232,22 +238,22 @@ def __init__(self, field, *args, **kwargs):
 
         # These are set in Django or in the global BOOTSTRAP3 settings, and
         # they can be overwritten in the template
-        error_css_class = kwargs.get('error_css_class', '')
-        required_css_class = kwargs.get('required_css_class', '')
-        bound_css_class = kwargs.get('bound_css_class', '')
-        if error_css_class:
+        error_css_class = kwargs.get('error_css_class', None)
+        required_css_class = kwargs.get('required_css_class', None)
+        bound_css_class = kwargs.get('bound_css_class', None)
+        if error_css_class is not None:
             self.error_css_class = error_css_class
         else:
             self.error_css_class = getattr(
                 field.form, 'error_css_class',
                 get_bootstrap_setting('error_css_class'))
-        if required_css_class:
+        if required_css_class is not None:
             self.required_css_class = required_css_class
         else:
             self.required_css_class = getattr(
                 field.form, 'required_css_class',
                 get_bootstrap_setting('required_css_class'))
-        if bound_css_class:
+        if bound_css_class is not None:
             self.success_css_class = bound_css_class
         else:
             self.success_css_class = getattr(


Test patch:
diff --git a/bootstrap3/tests.py b/bootstrap3/tests.py
--- a/bootstrap3/tests.py
+++ b/bootstrap3/tests.py
@@ -290,6 +290,51 @@ def test_buttons_tag(self):
         self.assertIn('col-md-3', res)
         self.assertIn('col-md-9', res)
 
+    def test_error_class(self):
+        form = TestForm({'sender': 'sender'})
+        res = render_template('{% bootstrap_form form %}', form=form)
+        self.assertIn('bootstrap3-err', res)
+
+        res = render_template(
+            '{% bootstrap_form form error_css_class="successful-test" %}',
+            form=form
+        )
+        self.assertIn('successful-test', res)
+
+        res = render_template('{% bootstrap_form form error_css_class="" %}',
+                              form=form)
+        self.assertNotIn('bootstrap3-err', res)
+
+    def test_required_class(self):
+        form = TestForm({'sender': 'sender'})
+        res = render_template('{% bootstrap_form form %}', form=form)
+        self.assertIn('bootstrap3-req', res)
+
+        res = render_template(
+            '{% bootstrap_form form required_css_class="successful-test" %}',
+            form=form
+        )
+        self.assertIn('successful-test', res)
+
+        res = render_template('{% bootstrap_form form required_css_class="" %}',
+                              form=form)
+        self.assertNotIn('bootstrap3-req', res)
+
+    def test_bound_class(self):
+        form = TestForm({'sender': 'sender'})
+        res = render_template('{% bootstrap_form form %}', form=form)
+        self.assertIn('bootstrap3-bound', res)
+
+        res = render_template(
+            '{% bootstrap_form form bound_css_class="successful-test" %}',
+            form=form
+        )
+        self.assertIn('successful-test', res)
+
+        res = render_template('{% bootstrap_form form bound_css_class="" %}',
+                              form=form)
+        self.assertNotIn('bootstrap3-bound', res)
+
 
 class FieldTest(TestCase):
     def test_illegal_field(self):