Skip to content

Commit 60f8103

Browse files
committed
MAGETWO-54186: Problem with js validation when adding product to shopping cart
- JS inline module become a separate widget
1 parent f505c20 commit 60f8103

File tree

3 files changed

+47
-23
lines changed

3 files changed

+47
-23
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ 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'
20+
catalogAddToCart: 'Magento_Catalog/js/catalog-add-to-cart',
21+
productValidate: 'Magento_Catalog/js/validate-product'
2122
}
2223
}
2324
};

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

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,11 @@
5151
}
5252
</script>
5353
<?php else : ?>
54-
<script>
55-
require([
56-
'jquery',
57-
'mage/mage',
58-
'Magento_Catalog/product/view/validation',
59-
'Magento_Catalog/js/catalog-add-to-cart'
60-
], function ($) {
61-
'use strict';
62-
63-
$('#product_addtocart_form').mage('validation', {
64-
radioCheckboxClosest: '.nested',
65-
submitHandler: function (form) {
66-
var widget = $(form).catalogAddToCart({
67-
bindSubmit: false
68-
});
69-
70-
widget.catalogAddToCart('submitForm', $(form));
71-
72-
return false;
73-
}
74-
});
75-
});
54+
<script type="text/x-magento-init">
55+
{
56+
"#product_addtocart_form": {
57+
"productValidate": {}
58+
}
59+
}
7660
</script>
7761
<?php endif; ?>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright © 2016 Magento. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
define([
6+
'jquery',
7+
'mage/mage',
8+
'Magento_Catalog/product/view/validation',
9+
'catalogAddToCart'
10+
], function($) {
11+
"use strict";
12+
13+
$.widget('mage.productValidate', {
14+
15+
options: {
16+
bindSubmit: false,
17+
radioCheckboxClosest: '.nested'
18+
},
19+
20+
_create: function() {
21+
var self = this;
22+
23+
this.element.mage('validation', {
24+
radioCheckboxClosest: this.options.radioCheckboxClosest,
25+
26+
submitHandler: function (form) {
27+
var widget = $(form).catalogAddToCart({
28+
bindSubmit: self.options.bindSubmit
29+
});
30+
31+
widget.catalogAddToCart('submitForm', $(form));
32+
return false;
33+
}
34+
});
35+
}
36+
});
37+
38+
return $.mage.productValidate;
39+
});

0 commit comments

Comments
 (0)