2

When I do in checkout, I got an error : I would like to detect if the person has filled in the registration form in the checkout, this code is added in this file : app/design/frontend/Theme/theme/Amasty_CheckoutCore/templates/onepage.phtml

define(['jquery','mage/translate', 'mage/validation'], function($){
    "use strict";
    return {
        customMain: function (){

            console.log($('.form-shipping-address').validation('isValid'))


        }
    }
});

error :

Uncaught Error: cannot call methods on validation prior to initialization; attempted to call method 'isValid'

4
  • Magento default support validates the shipping address form. What are you trying to do? Could you describe it in more detail?
    – Tu Van
    Commented Nov 3, 2022 at 11:32
  • @TuVan I would like to add an extra step in full javascript, and then I have to activate or deactivate a button by checking if the form is well filled or not
    – Kevin ami
    Commented Nov 3, 2022 at 11:33
  • Could you add the related xml, JS file, and a screenshot on the checkout page?
    – Tu Van
    Commented Nov 3, 2022 at 11:43
  • @TuVan I updated the code, I added the code in this file : app/design/frontend/Theme/theme/Amasty_CheckoutCore/templates/onepage.phtml
    – Kevin ami
    Commented Nov 3, 2022 at 13:31

2 Answers 2

0

Place your code inside require and load mage/validation

require([
'jquery',
'mage/validation'
], function($){
   
   // Place your other code here

   console.log($('.form-shipping-address').validation('isValid'))

 
});
2
  • this is what I did
    – Kevin ami
    Commented Nov 3, 2022 at 13:02
  • It puts an error in the console
    – Kevin ami
    Commented Nov 3, 2022 at 13:09
0

You have to run validation() function before use isValid.

Use this code to validate the shipping form:

var shippingForm = $('#co-shipping-form');
if (shippingForm.validation() && shippingForm.validation('isValid')) {
    console.log('valid');
} else {
    console.log('invalid');
}
3
  • I tried your solution in a setInterval, but console say valid while the form is not completed, don't understand why
    – Kevin ami
    Commented Nov 4, 2022 at 16:31
  • I tried also shippingForm.validate(), it does not detect errors
    – Kevin ami
    Commented Nov 8, 2022 at 9:11
  • Ok, let me check.
    – Tu Van
    Commented Nov 8, 2022 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.