1

I've created a custom validator following this official guide but I'm getting this error and the checkout page gets broken:

"script error for: StoreLocator/js/view/store-code-validator"

I have added in the composer.json module:

"require": {
    "magento/module-checkout": "100.1.*"
}

In StoreLocator/view/frontend/web/js/model/validate-store-code.js:

define(
    [],
    function () {
        'use strict';
        return {
            /**
             * Validate store code before placing any order
             *
             * @returns {boolean}
             */
            validate: function() {
                return true;
            }
        }
    }
);

In StoreLocator/view/frontend/web/js/view/store-code-validator.js:

define(
[
    'uiComponent',
    'Magento_Checkout/js/model/payment/additional-validators',
    'StoreLocator_StoreLocator/js/model/validate-store-code'
],
function (Component, additionalValidators, validateStoreCode) {
    'use strict';
    additionalValidators.registerValidator(validateStoreCode);
    return Component.extend({});
});

Then I call the validation in StoreLocator/view/frontend/layout/checkout_index_index.xml like this:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="checkout.root">
            <arguments>
                <argument name="jsLayout" xsi:type="array">
                    <item name="components" xsi:type="array">
                        <item name="checkout" xsi:type="array">
                            <item name="children" xsi:type="array">
                                <item name="steps" xsi:type="array">
                                    <item name="children" xsi:type="array">
                                        <item name="billing-step" xsi:type="array">
                                            <item name="children" xsi:type="array">
                                                <item name="payment" xsi:type="array">
                                                    <item name="children" xsi:type="array">
                                                        <item name="additional-payment-validators" xsi:type="array">
                                                            <item name="children" xsi:type="array">
                                                                <!-- Declare your validation. START -->
                                                                <item name="validate-store-code" xsi:type="array">
                                                                    <item name="component" xsi:type="string">
                                                                        StoreLocator_StoreLocator/js/view/store-code-validator
                                                                    </item>
                                                                </item>
                                                                <!-- Declare your validation. END -->
                                                            </item>
                                                        </item>
                                                    </item>
                                                </item>
                                            </item>
                                        </item>
                                    </item>
                                </item>
                            </item>
                        </item>
                    </item>
                </argument>
            </arguments>
        </referenceBlock>
    </body>
</page>
1

My problem was at the XML. I didn't notice the call of the validation had empty spaces. Removed the spaces inside the tag and it worked.

<item name="component" xsi:type="string">StoreLocator_StoreLocator/js/view/store-code-validator</item>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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