Skip to content
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

Is it possible to add is-valid class after validation #113

Open
xflyer11 opened this issue Sep 1, 2021 · 3 comments
Open

Is it possible to add is-valid class after validation #113

xflyer11 opened this issue Sep 1, 2021 · 3 comments
Labels

Comments

@xflyer11
Copy link

xflyer11 commented Sep 1, 2021

I understand there is a way to add error css class, is there a way to add the is-valid class after validation?

@zodman zodman added the question label Sep 2, 2021
@zodman
Copy link
Member

zodman commented Sep 2, 2021

{%if form.is_valid %}
{{form.field|add_class="is-valid"}}
{% endif %}

@fdemmer
Copy link

fdemmer commented Sep 11, 2021

with render_field i am currently doing it like this:

{% if form.myfield.errors %}
    {% render_field form.myfield class+="form-control is-invalid" title=form.myfield.label %}
{% else %}
    {% render_field form.myfield class+="form-control" title=form.myfield.label %}
{% endif %}

lot's of repeated code. is there a better way?

i'd imagine something like an "error_class" argument would be nice, eg:

{% render_field form.myfield class+="form-control" error_class="is-invalid" title=form.myfield.label %}

@BoPeng
Copy link

BoPeng commented May 2, 2022

I found this ticket right after submitting #126 , as far as I can tell,

{% if form.myfield.errors %}
    {% render_field form.myfield class+="form-control is-invalid" title=form.myfield.label %}
{% else %}
    {% render_field form.myfield class+="form-control" title=form.myfield.label %}
{% endif %}

does not handle the case of valid field from validated form, and we need something like

{% if form.myfield.errors %}
    {% render_field form.myfield class+="form-control is-invalid" title=form.myfield.label %}
{% elif form.is_bound %}
    {% render_field form.myfield class+="form-control is-valid" title=form.myfield.label %}
{% else %}
    {% render_field form.myfield class+="form-control" title=form.myfield.label %}
{% endif %}

This is quite tedious if we are doing this for all fields, #126 proposes the shortening of the above to

{% render_field form.myfield|add_class:"form-control"|add_valid_class:"form-control is-valid"|add_error_class:"form-control is-invalid" title=form.myfield.label %}

or even better

{% with WIDGET_ERROR_CLASS='is-invalid' WIDGET_VALID_CLASS="is-valid" %}
    {% render_field form.field1|add_class:"form-control" %}
{% endwith %}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants