Opened 13 years ago

Closed 12 years ago

#17051 closed Bug (fixed)

RegexValidator.message is not used

Reported by: jbcurtincode@… Owned by: anonymous
Component: Forms Version: 1.3
Severity: Normal Keywords: validator
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Error_messages works correctly, however with and with out error_messages, RegexValidator.message never pops up.

class UserIdForm(forms.Form):
    user_id = forms.CharField( max_length=50,
        label = _("Id"),
        help_text = _("Enter your ID."),
        error_messages = {
            'invalid':_(u"Your ID may only contain letters and numbers."),
            'required':_(u"Enter your ID."),
        },
        validators=[
            RegexValidator( 
                            regex='^[a-zA-Z0-9]*$',
                            message=_(u"Forgotten message."),
                            )
            ]
    )
{% for field in form %}
    {{field}}{{field.help_text}}
    <br/>
    {% for error in field.errors %}
        {{error}}
    {% endfor %}
{% endfor %}

Attachments (1)

17051.diff (4.0 KB ) - added by Claude Paroz 13 years ago.
Unset 'invalid' error_message on base Field class

Download all attachments as: .zip

Change History (10)

comment:1 by Claude Paroz, 13 years ago

There is an obvious conflict between the 'invalid' error message from the field and the validator (which default to the 'invalid' key also). If you specify an 'invalid' error message at field level, it will overwrite the 'invalid' error message from the Validator, by current design. In my opinion, it's not a bug.

in reply to:  1 ; comment:2 by anonymous, 13 years ago

Replying to claudep:

There is an obvious conflict between the 'invalid' error message from the field and the validator (which default to the 'invalid' key also). If you specify an 'invalid' error message at field level, it will overwrite the 'invalid' error message from the Validator, by current design. In my opinion, it's not a bug.

You're absolutely right, but in the absence of the 'invaild' error message the bug persists. The message property is never used.
https://docs.djangoproject.com/en/dev/ref/validators/#regexvalidator

I will update the ticket to convey this more accurately.

comment:3 by anonymous, 13 years ago

Owner: changed from nobody to jburtin

comment:4 by anonymous, 13 years ago

Owner: changed from jburtin to anonymous

in reply to:  2 comment:5 by anonymous, 13 years ago

Replying to anonymous:

Replying to claudep:

There is an obvious conflict between the 'invalid' error message from the field and the validator (which default to the 'invalid' key also). If you specify an 'invalid' error message at field level, it will overwrite the 'invalid' error message from the Validator, by current design. In my opinion, it's not a bug.

You're absolutely right, but in the absence of the 'invaild' error message the bug persists. The message property is never used.
https://docs.djangoproject.com/en/dev/ref/validators/#regexvalidator

I will update the ticket to convey this more accurately.

Well, I can't seem to edit the ticket since I forgot to login when I made it.

To recreate the bug with the code above, comment out the invalid error_message.

comment:6 by Claude Paroz, 13 years ago

Well, I investigate this issue a little more. The point is about what between field 'invalid' message and validator 'invalid' error message will take precedence. As you pointed out, the field message is currently always winning. Now if you want the validator message to win, you have to provide another error code, like 'invalid_regex':

RegexValidator( 
    regex='^[a-zA-Z0-9]*$',
    message=_(u"Forgotten message."),
    code='invalid_regex',
)

I tried to see if the code could be changed so as to choose the more specific message. But it is not possible to determine which is the most specific. Sometimes the validator error message is more specific, sometimes the field one is. That's why I think you should use the code trick above.

by Claude Paroz, 13 years ago

Attachment: 17051.diff added

Unset 'invalid' error_message on base Field class

comment:7 by Claude Paroz, 13 years ago

Has patch: set
Triage Stage: UnreviewedAccepted

Eventually I still found that the 'invalid' key of the base Field class was masking some useful validator messages. So I'm suggesting the above patch to improve this issue. The trick I described in comment:6 is still useful in case of specific Fields which define a default_error_messages['invalid'] entry.

comment:8 by Claude Paroz, 13 years ago

Component: Core (Other)Forms

#17679 is a duplicate with a link to a Github branch for a fix (https://github.com/insin/django/compare/validators-error-messages).

comment:9 by Claude Paroz <claude@…>, 12 years ago

Resolution: fixed
Status: newclosed

In 2f121dfe635b3f497fe1fe03bc8eb97cdf5083b3:

Fixed #17051 -- Removed some 'invalid' field error messages

When the 'invalid' error message is set at field level, it masks
the error message raised by the validator, if any.

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