Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add prioritization of configured error message over remote result. #547

Closed
wants to merge 8 commits into from

2 participants

@earino

There needs to be a way to say: "no matter what the remote call gives you, please use the error message that I provided, instead of the response from the back end." I do this by saying, if the error message configured starts with an exclamation mark (!), use it (after removing the exclamation mark), otherwise, feel free to use the response.

I am sorry I closed the previous pull request, I did not realize I could simply re-push my changes to it :-( I need to learn more about how github works.

Eduardo Arin... added some commits
Eduardo Arino de la Rubia If the error message for a field starts with an !, use it (without th…
…e !) instead of the result

from the remote call.
ed6b96e
Eduardo Arino de la Rubia Actually works now, misunderstood how the match function worked in JS. c649716
Eduardo Arino de la Rubia Get rid of debugger call 6fa793c
@earino

Just verified that yes, indeed, re-pushing to my repo does update the pull request.

Eduardo Arino de la Rubia Added semicolon as per travis-ci aa303b9
@jzaefferer
Owner

Phantomjs timing out is probably not your fault, so we may ignore that. Still, your PR would need a unit test to be mergable.

Once that is in, you should also rebase your commits into a single one.

@earino
@earino

Ok, it builds and i added a unit test. I'm so very sorry this is such a mess :(

@jzaefferer
Owner

Thanks for the effort, we're getting there.

It loos like your editor changed a lot of whitespace in the test? The diff should have only your test, no other changes.

@earino

My effort < Your patience!

Yeah, looks like something wacky like that happened. Let me get that fixed, 'cause that's nonsense.

Eduardo Arin... added some commits
Eduardo Arino de la Rubia Fixed spacing issues. 8693a0b
Eduardo Arino de la Rubia Fixed syntax error 1abbdb5
@earino

Ok, let me know if there is anything else you require!

@jzaefferer
Owner

I should've looked at the actual change earlier. What's your usecase that you need this API change? It sounds like you're dealing with a server response that you can't change on the server, but maybe you can just use the dataFilter option to clean up the response?

@earino

The use case is that there exists a class of messages for which our back end is giving horrible messages. Our front end people want a nice simple way of saying: "for this particular field, please just use the error message I provide, ignore what's coming from the server." Is that something that can be done easily with dataFilter?

@jzaefferer
Owner

So this works:

rules: {
    email: {
        remote: {
            url: "emails.php",
            dataFilter: function( response ) {
                if ( /err my gerd/.test( response ) ) {
                    return "\"better error message\"";
                }
                return response;
            }
        }
    }
}

Its a little clunky that you have to return a string escaped within a string, as the result is still parsed as JSON.

What do you think?

@jzaefferer jzaefferer referenced this pull request
Open

Meta: Documentation issues #656

0 of 2 tasks complete
@jzaefferer
Owner

The dataFilter approach seems okay for this special case. I've added added it to the list of things to document in #656.

@jzaefferer jzaefferer closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 12, 2012
  1. If the error message for a field starts with an !, use it (without th…

    Eduardo Arino de la Rubia authored
    …e !) instead of the result
    
    from the remote call.
  2. Actually works now, misunderstood how the match function worked in JS.

    Eduardo Arino de la Rubia authored
  3. Get rid of debugger call

    Eduardo Arino de la Rubia authored
  4. Added semicolon as per travis-ci

    Eduardo Arino de la Rubia authored
  5. Added appropriate unit tests and validated that the defaultMessage ha…

    root authored
    …s what it needs.
  6. Fixed test/methods.js which i screwed up earlier.

    Eduardo Arino de la Rubia authored
  7. Fixed spacing issues.

    Eduardo Arino de la Rubia authored
  8. Fixed syntax error

    Eduardo Arino de la Rubia authored
This page is out of date. Refresh to see the latest.
Showing with 48 additions and 1 deletion.
  1. +7 −1 jquery.validate.js
  2. +41 −0 test/methods.js
View
8 jquery.validate.js
@@ -1044,7 +1044,13 @@ $.extend($.validator, {
validator.showErrors();
} else {
var errors = {};
- var message = response || validator.defaultMessage( element, "remote" );
+ var defaultMessage = validator.defaultMessage( element, "remote" );
+
+ if (defaultMessage && typeof defaultMessage.match === 'function' && defaultMessage.match(/^!/)) {
+ response = defaultMessage.replace("!", "");
+ }
+
+ var message = response || defaultMessage;
errors[element.name] = previous.message = $.isFunction(message) ? message(value) : message;
validator.invalid[element.name] = true;
validator.showErrors(errors);
View
41 test/methods.js
@@ -408,6 +408,47 @@ test("remote", function() {
strictEqual( v.element(e), true, "still invalid, because remote validation must block until it returns; dependency-mismatch considered as valid though" );
});
+test("remote, with high priority message", function() {
+ expect(7);
+ stop();
+ var e = $("#username");
+ var v = $("#userForm").validate({
+ rules: {
+ username: {
+ required: true,
+ remote: "users.php"
+ }
+ },
+ messages: {
+ username: {
+ required: "Please",
+ remote: "!override-message"
+ }
+ },
+ submitHandler: function() {
+ ok( false, "submitHandler may never be called when validating only elements");
+ }
+ });
+ $(document).ajaxStop(function() {
+ $(document).unbind("ajaxStop");
+ equal( 1, v.size(), "There must be one error" );
+ equal( "override-message", v.errorList[0].message );
+
+ $(document).ajaxStop(function() {
+ $(document).unbind("ajaxStop");
+ equal( 1, v.size(), "There must be one error" );
+ equal( "override-message", v.errorList[0].message );
+ start();
+ });
+ e.val("Peter2");
+ strictEqual( v.element(e), true, "new value, new request; dependency-mismatch considered as valid though" );
+ });
+ strictEqual( v.element(e), false, "invalid element, nothing entered yet" );
+ e.val("Peter");
+ strictEqual( v.element(e), true, "still invalid, because remote validation must block until it returns; dependency-mismatch considered as valid though ");
+});
+
+
test("remote, customized ajax options", function() {
expect(2);
stop();
Something went wrong with that request. Please try again.