Skip to content

Core: Adding a way to pass method name to remote #1657

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

Closed
wants to merge 1 commit into from

Conversation

wojwal
Copy link
Contributor

@wojwal wojwal commented Dec 1, 2015

This allows reusing remote as custom method via addMethod. Without this fix it will show standard "remote" message - instead of custom message from addMethod.

Sample usage:

    $.validator.addMethod('workemail', function(value, element, param) {
        return $.validator.methods.remote.call(this, value, element, {
            url: "validate/workemail/"
        }, 'workemail');
    }, 'work email custom message');

And in HTML I'm just using data-rule-workemail='true' attribute for input

@wojwal wojwal force-pushed the master branch 2 times, most recently from 1d89469 to 8d96f00 Compare December 2, 2015 11:23
@wojwal
Copy link
Contributor Author

wojwal commented Jan 13, 2016

Hi, just wanted to get an update on this pull-request? :)

@staabm
Copy link
Member

staabm commented Jan 13, 2016

@wojwal thx for the PR. It somehow slipped thru :-)

I like it. Could you rebase it and add unit tests?

@Arkni
Copy link
Member

Arkni commented Jan 13, 2016

I'm not against this approach, but can't we achieve the same result if we use: data-rule-remote="validate/workemail/" and data-msg-remote="work email custom message" ?

Please correct me if I misunderstood something.

@wojwal
Copy link
Contributor Author

wojwal commented Jan 14, 2016

@staabm - I just rebased the code - so that part is ready. As for test units - I reviewed "test" folder, but it will take me some time to understand, how to do it properly - I'll do it when I got some spare time.

@Arkani - Not sure I guess your solution will work, but there is much more you can do with solution I proposed. For example:

  • you can have multiple custom methods in one page that uses remote - I think in the way you suggest you can have only 1 remote method with 1 defined message?
  • you can pass custom parameters as "data" to XHR requet
  • I use Javascript localization - so error message I passed to validator is additionally translated via Javascript - you wouldn't be able to do it with data-msg-remote attribute
  • HTML-wise - it's simpler just to have single attribute: "data-rule-workemail='params'" ;-)

I guess it's always better to have multiple possibilities ;)

@Arkni
Copy link
Member

Arkni commented Jan 14, 2016

So your idea is using the remote rule inside other custom rules and use the error message of your rule as the one for the remote method. I like it :)

Feel free to tag me whenever you need any help regarding the unit test :)

By the way my username is @Arkni :D

if ( this.optional( element ) ) {
return "dependency-mismatch";
}

var previous = this.previousValue( element ),
method = typeof method === "string" ? method : "remote";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this can be changed to:

method = method || "remote";

@wojwal wojwal force-pushed the master branch 2 times, most recently from 22965c5 to 6345f73 Compare January 17, 2016 10:16
@wojwal
Copy link
Contributor Author

wojwal commented Jan 17, 2016

Hi guys ;)

I've added tests for this, which includes:

  • checking error message passed to addMethod
  • additionally passing & requiring special parameter to XHR via data-rule-workemail attribute

@Arkni - as for your suggestion - I'm fine with either case. Your suggestion is just little less strict (not checking if value is string) - but thats not a big deal...

@Arkni
Copy link
Member

Arkni commented Jan 17, 2016

as for your suggestion - I'm fine with either case. Your suggestion is just little less strict (not checking if value is string) - but thats not a big deal...

I know, but what will happen if a user passed an empty String (in a test for example) ?
So for a strict check, you could use:

method = typeof method === "string" && method || "remote";

Otherwise, LGTM :)

@wojwal
Copy link
Contributor Author

wojwal commented Jan 18, 2016

I just commited that change ;)

@wojwal
Copy link
Contributor Author

wojwal commented Jan 22, 2016

HI guys, pls merge my changes... Anything missing?

Thanks

@staabm staabm removed the NEEDS TEST label Jan 28, 2016
@staabm
Copy link
Member

staabm commented Jan 28, 2016

@wojwal please rebase the branch against master. After I merge some other PRs today it is no longer mergeable. Will merge after your rebase ASAP. thanks.

This allows reusing remote as custom method via addMethod
@wojwal
Copy link
Contributor Author

wojwal commented Jan 29, 2016

@staab rebased ;)

@staabm staabm closed this in 4101b89 Feb 1, 2016
@staabm
Copy link
Member

staabm commented Feb 1, 2016

@wojwal thx

@Maks3w
Copy link
Contributor

Maks3w commented Jun 4, 2016

Just note this introduce a BC Break for those developers with custom validation functions.

This should be highlighted in the CHANGELOG

@Arkni
Copy link
Member

Arkni commented Jun 4, 2016

Just note this introduce a BC Break ...

How so ?

@Maks3w
Copy link
Contributor

Maks3w commented Jun 5, 2016

If the additional argument is not provided then method is undefined

When {method: undefined} is sent to defaultMessage app blow up because cannot do method.charAt

@Maks3w
Copy link
Contributor

Maks3w commented Jun 5, 2016

So if previousValue method is not set then the value should fallback to "remote"

@staabm
Copy link
Member

staabm commented Jun 5, 2016

@Maks3w please open a new issue

@Arkni
Copy link
Member

Arkni commented Jun 5, 2016

If the additional argument is not provided then method is undefined

No, that's not true. If the argument is not provided, then method will be set to remote prior to any call to previousValue, see https://github.com/jzaefferer/jquery-validation/blob/73c173198c2ab225ecb0713c55f509b30acbb9c0/src/core.js#L1426-L1428

@Maks3w
Copy link
Contributor

Maks3w commented Jun 5, 2016

Just note this introduce a BC Break for those developers with custom validation functions.

@Arkni
Copy link
Member

Arkni commented Jun 6, 2016

Sorry, but I still didn't get it.

Maybe you can open an issue and provide a simplified example (in jsfiddle or something else).
Thanks!

@Maks3w
Copy link
Contributor

Maks3w commented Jun 6, 2016

No need of jsfiddle previousValue is public and their signature must not change in unsafe way.

publicFunction(oldArg, newArg) { if (newArg === undefined) newArg = defaultValue }

@Arkni
Copy link
Member

Arkni commented Jun 6, 2016

Ah, I see. Thanks for the explanation.

This should be fixed as soon as possible, minor releases should never introduce breaking changes.

@staabm
Copy link
Member

staabm commented Jun 11, 2016

@Arkni I would like to release 15.1 next week, could you provide a fix for this until then?

@Maks3w
Copy link
Contributor

Maks3w commented Jun 11, 2016

One note defaultmessage has changed his behavior and the second argument must be an object. I'll put the note on the original PR if I found,

@Maks3w
Copy link
Contributor

Maks3w commented Jun 11, 2016

@Arkni
Copy link
Member

Arkni commented Jun 11, 2016

@Arkni I would like to release 15.1 next week, could you provide a fix for this until then?

Sure!

Arkni added a commit to Arkni/jquery-validation that referenced this pull request Jun 11, 2016
Arkni added a commit to Arkni/jquery-validation that referenced this pull request Jun 11, 2016
Arkni added a commit to Arkni/jquery-validation that referenced this pull request Jun 11, 2016
Arkni added a commit to Arkni/jquery-validation that referenced this pull request Jun 14, 2016
Arkni added a commit to Arkni/jquery-validation that referenced this pull request Jun 14, 2016
staabm pushed a commit that referenced this pull request Jul 15, 2016
As of now the signature of `validator.defaultMessage( element, string )`
is deprecated. Use `validator.defaultMessage( element, object )` instead
where objet is defined as
```
object = {
   method: "method name",
   parameters: "the given method parameters"
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants