question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom error messages as data attribute

See original GitHub issue

Hi there,

It would be really nice if you could add a custom error message as a data attribute to a field.

I would assume that data-bouncer-message would do the trick but it only works when a pattern fails, as you can see from this fiddle:

https://jsfiddle.net/rsx83ouw/5/

Other than that, a great library!

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
montemartincommented, Feb 12, 2020

I just deployed this on a project and [data-bouncer-message] worked on all fields. Trying it on another and it doesn’t work. The one that worked was using the CDN and loading v1.0.5. The non-working project I used downloaded v1.4.6. My conclusion is that it worked on all fields as a data attribute originally, and has since been changed.

1reaction
iamkeircommented, Nov 28, 2022

You can create your own custom attributes, see the demo in the docs: https://codepen.io/cferdinandi/pen/ywMdKp

Specifically, line 22 of the demo JS:

messages: {
  valueMismatch: function (field) {
    var customMessage = field.getAttribute('data-bouncer-mismatch-message');
    return customMessage ? customMessage : 'Please make sure the fields match.'
  }
}

You can create any data-attr format/naming convention and values you wanted.

Another example:

messages: {
  missingValue: {
    default: function(field) { return field.getAttribute('data-required-msg') ? field.getAttribute('data-required-msg') : 'Required'; }
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Data Attributes for Custom Validation Messages in HTML5
We can use field DATA attributes to customize our error messages and show a different one in each error case. Data attributes are...
Read more >
How to use custom attribute parameter in error message in ...
Hi,. I am writing custom validation attribute and it has parameter. Now I want to use that in error message but I am...
Read more >
jquery validate custom error element with data message attribute
I solved this problem by first checking if the error div already exists: errorPlacement: function (error, element) { error ...
Read more >
Form required attribute with a custom validation message in ...
We first use the checkValidity() method to check if the input fields contain valid data and run checkValidity() on form submit in order...
Read more >
How to create custom error rule using request attribute value ...
We have 200 error codes captured in a request attribute. In which some are considered Technical errors and some are businesses errors. We...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found