SetValidationValues ​​in jquery.validate.unobtrusive is not available outside of the file?

I extend MVC 3 validation with custom server-side ValidationAttributes and have implemented IClientValidatable to output html5 data attributes for my custom validation. Then I used jQuery.validator.addMethod to add my client side validation. My final task is to write an adapter that converts html5 data attributes to a format that the jQuery validation engine understands. My check takes several values, so I need to use jQuery.validator.unobtrusive.adapters.add, and not one of the simpler functions like addSingleVal.

Inside my adapter, it would be useful to use several functions in the jquery.validate.unobtrusive file, such as setValidationValues ​​and getModelPrefix, but if I'm not mistaken, these functions are inside a closure, which makes them available only in this closure, I really don't want to put my user-defined functions in a script file owned by Microsoft, which can change, so if anyone doesn’t have any ideas, I have to replicate these functions in my own file, which is obviously not perfect.

So, any ideas and what are other people doing?

+4
source share
1 answer

I'm afraid not.
As you know, these functions are implemented as follows:

(function ($) { ... function setValidationValues(options, ruleName, value){ //the implementation } ... }(jQuery)) 

as long as this function is defined in a private area that you cannot access from the outside, this is a kind of private function for the class. The purpose of defining such functions is that you cannot access them outside the scope. minifier methods also know this FACT and use it to compress js as much as possible. if you look at the shortened version of jquery.validate.unobtrusive.min.js , then make sure that the setValidationValues function no longer exists, but (some kind of letter) x .

This is not a big copy and paste for these functions if they are too short to worry about their sizes. With the minifier you can get this function and other 3 other functions for only 200 bytes . and this 200 bytes will be used only once.


However, there is a way, if you use the mini version, you can find the name of the function and put it in your adapter.add(function(){//use their name here}) , you would notice that copying and pasting is better.
+2
source

Source: https://habr.com/ru/post/1336956/


All Articles