Skip to content

Commit 690c59d

Browse files
committed
MAGETWO-54186: Problem with js validation when adding product to shopping cart
- refactoring according to review
1 parent 60f8103 commit 690c59d

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

app/code/Magento/Catalog/view/frontend/requirejs-config.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ var config = {
1717
priceOptionFile: 'Magento_Catalog/js/price-option-file',
1818
priceOptions: 'Magento_Catalog/js/price-options',
1919
priceUtils: 'Magento_Catalog/js/price-utils',
20-
catalogAddToCart: 'Magento_Catalog/js/catalog-add-to-cart',
21-
productValidate: 'Magento_Catalog/js/validate-product'
20+
catalogAddToCart: 'Magento_Catalog/js/catalog-add-to-cart'
2221
}
2322
}
2423
};

app/code/Magento/Catalog/view/frontend/templates/product/view/addtocart.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
<script type="text/x-magento-init">
5555
{
5656
"#product_addtocart_form": {
57-
"productValidate": {}
57+
"Magento_Catalog/js/validate-product": {}
5858
}
5959
}
6060
</script>

app/code/Magento/Catalog/view/frontend/web/js/validate-product.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,37 @@ define([
77
'mage/mage',
88
'Magento_Catalog/product/view/validation',
99
'catalogAddToCart'
10-
], function($) {
11-
"use strict";
10+
], function ($) {
11+
'use strict';
1212

1313
$.widget('mage.productValidate', {
14-
1514
options: {
1615
bindSubmit: false,
1716
radioCheckboxClosest: '.nested'
1817
},
1918

20-
_create: function() {
21-
var self = this;
19+
/**
20+
* Use magento validation widget for the form object
21+
* @private
22+
*/
23+
_create: function () {
24+
var bindSubmit = this.options.bindSubmit;
2225

23-
this.element.mage('validation', {
26+
this.element.validation({
2427
radioCheckboxClosest: this.options.radioCheckboxClosest,
2528

29+
/**
30+
* Use catalogAddToCart widget as submit handler
31+
* @param {Object} form
32+
* @returns {Boolean}
33+
*/
2634
submitHandler: function (form) {
27-
var widget = $(form).catalogAddToCart({
28-
bindSubmit: self.options.bindSubmit
35+
var jqForm = $(form).catalogAddToCart({
36+
bindSubmit: bindSubmit
2937
});
3038

31-
widget.catalogAddToCart('submitForm', $(form));
39+
jqForm.catalogAddToCart('submitForm', jqForm);
40+
3241
return false;
3342
}
3443
});

0 commit comments

Comments
 (0)