Skip to content

add another filter add_valid_class? #126

@BoPeng

Description

@BoPeng

bootstrap allows the use of is-valid and is-invalid for fields of validated forms (bounded). Whereas is-invalid class can be easily added as

 {% render_field form.field1|add_error_class:'is-invalid' %}

It is not easy to do so with is-valid because blindly adding the class will mark fields as valid for unbounded forms.

Is it possible to add another filter called add_valid_class as follows?

@register.filter("add_valid_class")
@silence_without_field
def add_valid_class(field, css_class):
    if field.form.is_bound and not (hasattr(field, "errors") and field.errors):
        return add_class(field, css_class)
    return 

I suppose it also makes sense to add WIDGET_VALID_CLASS so that I can do

{% with WIDGET_ERROR_CLASS='is-invalid' WIDGET_VALID_CLASS="is-valid" %}
    {% render_field form.field1 %}
    {% render_field form.field2 %}
    {% render_field form.field3 %}
{% endwith %}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions