Magento2 - Require validation issue for a new checkbox on onepage checkout form

1.6k views Asked by At

I am using Magento 2.2.3 and i added a new checkbox field with require validation in address onepage checkout form by custom plugin. I follow this guide: https://devdocs.magento.com/guides/v2.0/howdoi/checkout/checkout_new_field.html

Now I display checkbox but validation does not work well. Require validation only works when customer uncheck the field after checking it.

I need validation to work even when checkbox has never been checked like on first load of the form

Below is my code of LayoutProcessor:

class LayoutProcessor
{
    /**
     * @param \Magento\Checkout\Block\Checkout\LayoutProcessor $subject
     * @param array $jsLayout
     * @return array
     */
    public function afterProcess(
        \Magento\Checkout\Block\Checkout\LayoutProcessor $subject,
        array  $jsLayout
    ) {
        $customAttributeCode = 'custom_field';
        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']
        ['shipping-address-fieldset']['children'][$customAttributeCode] = [
            'component' => 'Magento_Ui/js/form/element/single-checkbox',
            'config' => [
                'customScope' => 'shippingAddress.custom_attributes',     
                'template' => 'ui/form/field',
                'elementTmpl' => 'ui/form/components/single/checkbox',
            ],
            'label' => 'Bla bla bla bla.....',
            'dataScope' => 'shippingAddress.custom_attributes' . '.' . $customAttributeCode,
            'provider' => 'checkoutProvider',
            'sortOrder' => 251,
            'required' => true,
            'validation' => [
               'required-entry' => true
            ],
            'description'=>null,
            'value' => '1',
        ];
        return $jsLayout;
    }
}
0

There are 0 answers