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

Unable to override validationRequiredMessage #53

Closed
oliversalzburg opened this issue Jan 23, 2013 · 7 comments
Closed

Unable to override validationRequiredMessage #53

oliversalzburg opened this issue Jan 23, 2013 · 7 comments
Labels
Milestone

Comments

@oliversalzburg
Copy link

Even if I set data-validation-required-message on my input box, I still get the default error message.

The value is read just fine using $this.data("validationRequiredMessage"), yet, the value isn't used. I confirmed this at https://github.com/ReactiveRaven/jqBootstrapValidation/blob/1.3.6/jqBootstrapValidation.js#L164

While hunting for the source of this, I noticed something weird at https://github.com/ReactiveRaven/jqBootstrapValidation/blob/1.3.6/jqBootstrapValidation.js#L288 It looked to me like hasOverrideMessage is always true because the messages were set earlier. A simple alert(hasOverrideMessage) confirms this.

So, I'm somewhat confused about what's going on.

@oliversalzburg
Copy link
Author

Okay, part of this resolved itself. I forgot to add novalidate on the form and was seeing the Chrome validation bubble.

This was especially confusing because the example page exhibits the same problem

@ReactiveRaven
Copy link
Owner

Hi Oliver!

While hunting for the source of this, I noticed something weird at https://github.com/ReactiveRaven/jqBootstrapValidation/blob/1.3.6/jqBootstrapValidation.js#L288 It looked to me like hasOverrideMessage is always true because the messages were set earlier. A simple alert(hasOverrideMessage) confirms this.

If you do $("body").data("something-that-isnt-there") you should get back undefined, because that data-attribute isn't defined. In dev-master I've simplified this to var hasOverrideMessage = !!message; to avoid confusion.

The value is read just fine using $this.data("validationRequiredMessage"), yet, the value isn't used. I confirmed this at https://github.com/ReactiveRaven/jqBootstrapValidation/blob/1.3.6/jqBootstrapValidation.js#L164

The pertinent bit there:

message = settings.builtInValidators.required.message;
if ($this.data("validationRequiredMessage")) {
  message = $this.data("validationRequiredMessage");
}

overwrites the default with your message from the data-attribute, so if you can read it in jQuery, it should be using your message fine.

Okay, part of this resolved itself. I forgot to add novalidate on the form and was seeing the Chrome validation bubble.

This was especially confusing because the example page exhibits the same problem

I agree. I'm in the process of rewriting the docs, but its taking me far too long. I've been neglecting the current version of the docs. Sorry about that.

Could you highlight the part that hasn't resolved itsself? Are you seeing your validation message now?

Thanks

  • RRaven

@oliversalzburg
Copy link
Author

Hello @ReactiveRaven :)

As far as I can tell, my immediate problems were resolved. Adding the novalidate to my forms made everything behave as expected.

What wasn't resolved was the incorrect example on the documentation page, but you already touched on that subject.

I'm still confused regarding this section of the code though. As you've pointed out, you adjusted that code in master. However, to my understanding, my observations should still be true. If settings.options.sniffHtml is enabled, the message data members are explicitly set on all form elements: https://github.com/ReactiveRaven/jqBootstrapValidation/blob/master/dist/jqBootstrapValidation-1.3.6.js#L96.

For example, when putting required on an input, the data member will always be set. Thus, the hasOverrideMessage variable should always be true later on (even though the message wasn't actually overriden).

@ReactiveRaven
Copy link
Owner

Ah, I understand now.

Yes, its not ideal. The easiest way of forcing a validator to be used is make sure a message is set for it, so thats what it does when it finds the 'required' attribute. I ran into this problem in master when adding tests and just went the easy way of making custom/default messages work for sniffed attributes. It still works, because the default message is being put in as the data-attribute that is overriding the.. default message. Convoluted, but you get the right result.

Its something I want to tidy up for 2.0 but it passes tests in master right now. I'll be coming back to this once I've got back in control of the issues list. Thanks for highlighting it :)

@GuiDeto
Copy link

GuiDeto commented May 15, 2015

No description provided.

@FFDomm
Copy link

FFDomm commented Mar 8, 2016

Hello.

I have 2 textboxes one user and password.

sig

and even if the first one is ok, it keeps saying "enter signum" when it should be asking for password.

This is the code for the textboxes:
code

Thanks in advace

@FFDomm
Copy link

FFDomm commented Mar 8, 2016

I finally got it!

all i did was clear the custom validity property on each textbox

placeholder="Signum" required oninput="setCustomValidity('')" oninvalid="this.setCustomValidity('Please enter Signum')"

:)

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