I am trying to add a custom validation form with mixin.
I have created simple module Mageplaza_Helloworld
.
In \app\code\Mageplaza\HelloWorld\view\frontend\web\js\requirejs-config.js
:
<script type="text/javascript">
var config = {
config: {
mixins: {
'mage/validation': {
'Mageplaza_HelloWorld/js/validation-mixin': true
}
}
}
}
</script>
And in: \app\code\Mageplaza\HelloWorld\view\frontend\web\js\validation-mixin.js
:
<script type="text/javascript">
define([
'jquery'
], function ($) {
"use strict";
return function () {
$.validator.addMethod(
'validate-jurgis',
function (value) {
$.mage.__('Your validation error message');
return false;
},
);
}
});
</strict>
Im returing false
just to see if it works at all (which it doesnt)
And in vendor\magento\module-checkout\view\frontend\layout\checkout_index_index.xml
I assign it like:
<item name="telephone" xsi:type="array">
<item name="config" xsi:type="array">
<item name="tooltip" xsi:type="array">
<item name="description" xsi:type="string" translate="true">For delivery questions.</item>
</item>
</item>
<!-- add a validation code at here -->
<item name="validation" xsi:type="array">
<item name="validate-jurgis" xsi:type="string">true</item>
</item>
<!-- end of validation -->
</item>
(If I replace validate-jurgis
with an existing form, it works)
But it doesnt work. I get no errors and am able to proceed…
Attempt nr. 3
\app\code\Mageplaza\HelloWorld\view\base\web\js\requirejs-config.js
<script type="text/javascript">
var config = {
config: {
mixins: {
'Magento_Ui/view/base/web/js/lib/validation/rules': {
'Mageplaza_HelloWorld/js/validation-mixin': true
}
}
}
}
</script>
\app\code\Mageplaza\HelloWorld\view\base\web\js\validation-mixin.js
<script type="text/javascript">
define([
'mage/translate'
], function ($t) {
'use strict';
return function (rules) {
rules['validate-jurgis'] = {
handler: function () {
return false;
},
message: $t('My message')
};
return rules;
};
});
</script>
vendor\magento\module-checkout\view\frontend\layout\checkout_index_index.xml
<item name="validation" xsi:type="array">
<item name="validate-jurgis" xsi:type="string">true</item>
</item>
Best Answer
Hi @Jurģis Toms Liepiņš - your answer helped me a lot!
Multiple Validation Methods
It appears there are two JavaScript validation implementations:
1. mage/validation.js
The
mage/validation
file is referenced bymage/validation/validation
, which is mapped with RequireJS tovalidation
:Inchoo has an article on how to use this.
2. Magento_Ui/js/lib/validation/rules.js
The
Magento_Ui/js/lib/validation/rules.js
file is referenced inMagento_Ui/js/form/element/abstract
andMagento_Ui/js/form/element/file-uploader
and seems to be used to validate UI form elements.Example using this validation in XML layout:
Usage + Examples
Here is the necessary code to add custom validation rules to the two implementations:
Example RequireJS Configuration
Example
mage/validation
MixinExample
Magento_Ui/js/lib/validation/rules
MixinNote: Joel Davey shows another great way to add validation methods for UI elements