Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#30833 closed Cleanup/optimization (invalid)

Use field label when available to display errors in forms.

Reported by: Lorran Rosa Owned by: Lorran Rosa
Component: Forms Version: 3.0
Severity: Normal Keywords: forms, label, internationalization
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

The issue here is if you have a form field called "year" even if you set a label like "Ano" form.errors will display "year".
Which can be confusing for non-english speakers.

Attachments (1)

form_issue.png (2.3 KB ) - added by Lorran Rosa 5 years ago.
form.errors display this

Download all attachments as: .zip

Change History (8)

comment:1 by Mariusz Felisiak, 5 years ago

Has patch: unset
Resolution: needsinfo
Status: assignedclosed
Summary: Use field label when available to display errors in formsUse field label when available to display errors in forms.
UI/UX: unset
Version: 2.23.0

Thanks for this report, however I cannot find any builtin validator with message that contains field's name. Can you provide more details?

by Lorran Rosa , 5 years ago

Attachment: form_issue.png added

form.errors display this

comment:2 by Lorran Rosa , 5 years ago

Resolution: needsinfo
Status: closednew
UI/UX: set

Lets supose I have a form like this:

class WritersForm(forms.Form):

    name = forms.CharField(label='Your name', max_length=45)
    number = forms.IntegerField(
        widget=forms.TextInput(), 
        label='Número'
    )

I still want to validade number field but I don't like de IntergerField widget style so I use TextInput widget instead.

My template is like this:

{{ form.errors }}
<form action='{% url "create_writer" %}' method="POST">

    {% csrf_token %}
    <div class="form-group">

        {{ form.name.label_tag }}
        {{ form.name }}

        {{ form.number.label_tag }}
        {{ form.number }}

    </div>
    
    <input type="submit" class="btn bg-primary" value="Submit">
</form>

if I do not input a Interger in "number" field my form.errors returns:

number

    Enter a whole number.

Even with the field "label" argument defined in forms to "Número".

The desired behavior would be errors to use label field when available:

Número

    Enter a whole number.

comment:3 by Mariusz Felisiak, 5 years ago

Resolution: invalid
Status: newclosed

"Enter a whole number" is a translated message (for users) and number is a key in a dictionary with errors that maps fields to their errors. This is expected behavior (see documentation).

Please use one of support channels.

comment:4 by Lorran Rosa , 5 years ago

Resolution: invalid
Status: closednew

Got your point. But in not so simple forms as the one above it can be really confusing, let me elaborate:

class WritersForm(forms.Form):

    name = forms.CharField(label='имя', max_length=45)
    number = forms.IntegerField(
        widget=forms.TextInput(), 
        label='номер'
    ),
    year = forms.IntegerField(
        widget=forms.TextInput(), 
        label='год'
    ),
    house_number = forms.IntegerField(
        widget=forms.TextInput(), 
        label=Номер дома'
    ),

If my russian user mistyped something he/she will see this:

number

    Пожалуйста, введите правильный номер.

house_number

    Пожалуйста, введите правильный номер.

"year" also expects a "number" as input, if my user do not speak english how do it know which field was mistyped ?
What is seen in form is "номер", "год", "Номер дома".

comment:5 by Mariusz Felisiak, 5 years ago

Resolution: invalid
Status: newclosed

You can display errors related with fields directly with them

{{ form.number.errors.as_ul }}

Please don't reopen closed ticket and use support channels for support questions.

comment:6 by Lorran Rosa , 5 years ago

Resolution: invalidworksforme

Thanks for your time, It isn't the behavior I expected but meet my needs.

comment:7 by Mariusz Felisiak, 5 years ago

Resolution: worksformeinvalid
UI/UX: unset

Ticket is "invalid" because it was a support question not an issue in Django (see documentation).

Note: See TracTickets for help on using tickets.
Back to Top