cancel
Showing results for 
Search instead for 
Did you mean: 

Checkout - How to trigger validation on input fields that have been automatically filled?

Checkout - How to trigger validation on input fields that have been automatically filled?

I am building a postcode lookup for Magento 2.4.6' Checkout
I am at the stage where I have a functioning form, lookup, and input field fill. I can choose a postcode, choose and address and it will pre-fill the address fields on Shipping address.

 

The problem is; when I move the payment screen it says that the auto-filled are still 'required' and throws the error above the fields.

I have tried simulating a keypress event through js to trigger validation - no dice.

 

I thought this would be the way, except the only input field-sets that get sent through are billing and payment related (these are direct from the layout processor) and no shippingAddress what so ever.

 

As this point I figured I'd come here and see if anyone has a solid way of triggering form validation on a shipping address that has been pre-filled by a jQuery plugin (I am using getAddress.io) loaded via Require on onepage.phtml. It waits for certain elements to appear before triggering so it loads each time.

 

Cheers in advance

1 REPLY 1

Re: Checkout - How to trigger validation on input fields that have been automatically filled?

Hello @chriskiddier 

 

Trigger Magento’s Validation on the Shipping Form

After pre-filling the form, you should explicitly trigger Magento’s validation for the shipping form using JavaScript.

require([
    'jquery',
    'Magento_Checkout/js/model/checkout-data-resolver',
    'Magento_Checkout/js/action/validate-shipping-information'
], function ($, checkoutDataResolver, validateShippingInformationAction) {
    // Pre-fill the form using your jQuery plugin
   // Trigger change or input events on other fields as needed
    $('#shipping-new-address-form input[name="firstname"]').val('James').trigger('change');
    
    // Re-resolve the checkout data
    checkoutDataResolver.resolveShippingAddress();

    // Manually trigger validation on the shipping address form
    validateShippingInformationAction();
});

Hope it helps !

If you find our reply helpful, please give us kudos.

 

A Leading Magento Development Agency That Delivers Powerful Results, Innovation, and Secure Digital Transformation.

 

WebDesk Solution Support Team

Get a Free Quote | | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789

 

 

Thank You,


WebDesk Solution Support Team
Get a Free Quote | Email | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789


Location: 150 King St. W. Toronto, ON M5H 1J9