Opened 11 years ago
Closed 9 years ago
#17051 closed Bug (fixed)
RegexValidator.message is not used
Reported by: | 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)
Change History (10)
comment:1 follow-up: 2 Changed 11 years ago by
comment:2 follow-up: 5 Changed 11 years ago by
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 11 years ago by
Owner: | changed from nobody to jburtin |
---|
comment:4 Changed 11 years ago by
Owner: | changed from jburtin to anonymous |
---|
comment:5 Changed 11 years ago by
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 11 years ago by
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 11 years ago by
Attachment: | 17051.diff added |
---|
Unset 'invalid' error_message on base Field class
comment:7 Changed 11 years ago by
Has patch: | set |
---|---|
Triage Stage: | Unreviewed → 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 10 years ago by
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 Changed 9 years ago by
Resolution: | → fixed |
---|---|
Status: | new → closed |
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.