In a custom module view/frontend/web/js
folder I have 2 JS files:
jquery.custom.js
registration.js
In the XML layout file of the module, I have:
<head>
<link src="Vendor_Module::js/jquery.custom.js"/>
<link src="Vendor_Module::js/registration.js"/>
</head>
Inside the third party library jquery.custom.js
file there is a function called $.validateData
. This library works since I use it everywhere in my projects (so there is no an issue with $.validateData
function definition). The problem is when trying to use it in Magento.
This is registration.js
file content:
requirejs([
'jquery',
'jquery/ui',
'jquery/validate',
'mage/translate',
'mage/mage',
], function ($) {
$.validator.addMethod(
'validateData',
function (value) {
return $.validateData(value);
},
$.mage.__('Data is incorrect.')
);
});
When I load the page and the custom validation is run, a javascript error is shown telling that $.validateData is not found
.
I think that the problem is because I am referencing the third party library incorrectly, so, the question is, how can I reference it correctly? I should add it in the requirejs
array? how?
I tried to add that library in the registration.js
file this way:
requirejs([
'jquery',
'jquery/ui',
'jquery/validate',
'mage/translate',
'mage/mage',
'Vendor_Module/js/jquery.custom.js'
], function ($) {
$.validator.addMethod(
'validateData',
function (value) {
return $.validateData(value);
},
$.mage.__('Data is incorrect.')
);
});
But in this case, this other error is shown:
require.js:166 Uncaught Error: Script error for: Vendor_Module/js/jquery.custom.js
http://requirejs.org/docs/errors.html#scripterror
at makeError (require.js:166)
at HTMLScriptElement.onScriptError (require.js:1681)
what is missing?
Thanks