Code

Opened 3 years ago

Closed 16 months 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 claudep 3 years ago.
Unset 'invalid' error_message on base Field class

Download all attachments as: .zip

Change History (10)

comment:1 follow-up: Changed 3 years ago by claudep

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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.

comment:2 in reply to: ↑ 1 ; follow-up: Changed 3 years ago by 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.

comment:3 Changed 3 years ago by anonymous

  • Owner changed from nobody to jburtin

comment:4 Changed 3 years ago by anonymous

  • Owner changed from jburtin to anonymous

comment:5 in reply to: ↑ 2 Changed 3 years ago by anonymous

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 Changed 3 years ago by claudep

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.

Changed 3 years ago by claudep

Unset 'invalid' error_message on base Field class

comment:7 Changed 3 years ago by claudep

  • Has patch set
  • Triage Stage changed from Unreviewed to Accepted

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 Changed 2 years ago by claudep

  • Component changed from Core (Other) to 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 Changed 16 months ago by Claude Paroz <claude@…>

  • Resolution set to fixed
  • Status changed from new to closed

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.