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.

Parsley data-parsley-error-message just presents global message within ul.parsley-errors-list

See original GitHub issue

What kind of issue is this? (put ‘x’ between the square brackets)

See details here

jsfiddle example

I put together this small patch locally to allow the global message to take precedence over other messages:

diff --git a/vendor/assets/javascripts/parsley.js b/vendor/assets/javascripts/parsley.js
index d34a2f5..7838509 100644
--- a/vendor/assets/javascripts/parsley.js
+++ b/vendor/assets/javascripts/parsley.js
@@ -1030,9 +1030,10 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
     },

     _getErrorMessage: function _getErrorMessage(constraint) {
-      var customConstraintErrorMessage = constraint.name + 'Message';
+      var customConstraintErrorMessage = this.options['errorMessage'] || this.options[constraint.name + 'Message'];

-      if ('undefined' !== typeof this.options[customConstraintErrorMessage]) return window.Parsley.formatMessage(this.options[customConstraintErrorMessage], constraint.requirements);
+      if ('undefined' !== typeof customConstraintErrorMessage)
+        return window.Parsley.formatMessage(customConstraintErrorMessage, constraint.requirements);

       return window.Parsley.getErrorMessage(constraint);
     },

The order of options in the assignment to customConstraintErrorMessage could be reversed if specific error messages should take precedence over global.

I am attempting to upgrade from Parsley v2.0.7; we have some poorly structured validations which specify both global and specific messages, but our tests assume global message has precedence since we had overridden ParsleyUI._getErrorMessage. That override no longer works with v2.8 and looking at the docs it appeared as though the data-parsley-error-message feature would work out-of-the-box. That’s how I wound up here.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
marcandrecommented, Nov 22, 2017

I’m afraid it won’t work. I’m sorry that Parsley object creation is a real kludge. I hope to fix it one day… In the meantime you can use another kludge instead: window.ParsleyExtend._getErrorMessage = ... (after loading Parsley).

1reaction
bwhite-mdsolcommented, Nov 21, 2017

Correct on the docs. Priority is not specified.

WRT precedence, taking a closer look at the code I removed from our extensions:

-
-  /**
-   * Override this method to allow using a catch-all default "errorMessage" attribute when displaying validation
-   * error messages if a specific error message for the failed constraint is not satisfied.
-   */
-  window.ParsleyUI._getErrorMessage = function(fieldInstance, constraint) {
-    var customConstraintErrorMessage = fieldInstance.options[constraint.name + 'Message'] ||
-      fieldInstance.options['errorMessage'];
-    if ('undefined' !== typeof customConstraintErrorMessage) {
-      return window.ParsleyValidator.formatMessage(customConstraintErrorMessage, constraint.requirements);
-    }
-    return window.ParsleyValidator.getErrorMessage(constraint);
-  };
-

I see that we had given priority to customConstraintErrorMessage. So the test failures must’ve been just because I ran the tests after I’d removed the code above from our extensions, but before adding it back via the patch posted previously.

I confirmed this by changing my current _getErrorMessages to:

    _getErrorMessage: function _getErrorMessage(constraint) {
      var customConstraintErrorMessage = this.options[constraint.name + 'Message'];
      var message = customConstraintErrorMessage || this.options['errorMessage'];

      if ('undefined' !== typeof message) return window.Parsley.formatMessage(message, constraint.requirements);

      return window.Parsley.getErrorMessage(constraint);
    },

and rerunning our tests. They pass.

Given this, I concur with your statement “customConstraintErrorMessage should get priority, with errorMessage being a catch all for other types…” and we actually have no issue with customConstraintErrorMessage taking higher priority.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Parsley data-parsley-error-message just presents global ...
According to the documentation, when clicking "Validate" here, I would expect the errors in the error list below the input and the errors...
Read more >
Parsley.js - Bountysource
Parsley data-parsley-error-message just presents global message within ul.parsley-errors-list $ 0. Created 5 years ago in guillaumepotier/Parsley.js with 8 ...
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