Skip to content

Field valid and invalid custom classes #739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Remove `<nav>` from pagination (#686, @xi).
- Add an `id` to the help text of fields for Django 5.0+, to match the `aria-describedby` attribute.
- Drop support for Python 3.8 in the test matrix
- Add field paramaters for custom valid and invalid classes

## 24.3 (2024-09-17)

Expand Down
7 changes: 5 additions & 2 deletions src/django_bootstrap5/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ def __init__(self, field, **kwargs):
else success_css_class
)

self.field_valid_class = kwargs.get("field_valid_class", "is-valid")
self.field_invalid_class = kwargs.get("field_invalid_class", "is-invalid")

@property
def is_floating(self):
return (
Expand Down Expand Up @@ -426,9 +429,9 @@ def get_errors_html(self):
def get_server_side_validation_classes(self):
"""Return CSS classes for server-side validation."""
if self.field_errors:
return "is-invalid"
return self.field_invalid_class
elif self.field.form.is_bound:
return "is-valid"
return self.field_valid_class
return ""

def get_inline_field_class(self):
Expand Down
10 changes: 10 additions & 0 deletions src/django_bootstrap5/templatetags/django_bootstrap5.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,16 @@ def bootstrap_field(field, **kwargs):

:default: ``'has-success'``. Can be changed :doc:`settings`

field_valid_class
CSS class used by the field when it is valid

:default: ``'is-valid'``.

field_invalid_class
CSS class used by the field when it is invalid

:default: ``'is-invalid'``

**Usage**::

{% bootstrap_field field %}
Expand Down
17 changes: 17 additions & 0 deletions tests/test_bootstrap_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ def test_field_class(self):
)
self.assertIn('class="form-control field-class-test"', html)

def test_valid_field_class(self):
form = SubjectTestForm({"subject": "valid_data"})
form.is_valid()
html = self.render("{% bootstrap_field form.subject field_valid_class='custom-class' %}", {"form": form})
self.assertNotIn('class="form-control is-valid"', html)
self.assertIn('class="form-control custom-class"', html)

def test_invalid_field_class(self):
form = SubjectTestForm({"invalid_field": "invalid_data"})
form.is_valid()
html = self.render(
"{% bootstrap_field form.subject field_invalid_class='custom-class' %}",
{"form": form},
)
self.assertNotIn('class="form-control is-invalid"', html)
self.assertIn('class="form-control custom-class"', html)

def test_xss_field(self):
html = self.render("{% bootstrap_field form.xss_field %}", {"form": XssTestForm()})
self.assertIn('type="text"', html)
Expand Down
Loading