cancel
Showing results for 
Search instead for 
Did you mean: 

How can I validate my custom checkbox on LayoutProcessor.php

How can I validate my custom checkbox on LayoutProcessor.php

HI!

I new in Magento and I'm trying to personalize my checkout page, I want to add a newsletter checkbox. I started to add this code in LayoutProcessor.php of checkout module :

 

 if (isset($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
            ['shippingAddress']['children']['shipping-address-fieldset']['children'])
        ) {
            $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
            ['shippingAddress']['children']['shipping-address-fieldset']['children']['subscribe'] = [
                'component' => 'Magento_Ui/js/form/element/abstract',
                'config' => [
                    'customScope' => 'shippingAddress',
                    'template' => 'ui/form/field',
                    'elementTmpl' => 'ui/form/element/checkbox',
                    'options' => [],
                    'id' => 'delivery-date',
                ],
                'dataScope' => 'shippingAddress.newsletter_subscribe',
                'label' => 'Sign Up for Newsletter',
                'provider' => 'checkoutProvider',
                'visible' => true,
                'checked' => true,
                'validation' => [],
                'sortOrder' => 250,
                'id' => 'newsletter-subscribe',
                'value' => 'subscription',
            ];
        }

And now I need to get the checkbox value and put the consumer email in newsletter_suscriber table after finish the checkout process. How Can I do that please ? 

Thank you! 

3 REPLIES 3

Re: How can I validate my custom checkbox on LayoutProcessor.php

Hi

I ended up replacing 'required-entry' with a custom validator that checks if the checkbox is checked first. For example:

 validator.addRule(
            'required-entry-if-create-account-checked',
            function (value) {
                if(hiddenLoginFields.is(':visible') || !$('input[name*="create_account_checkbox"]').is(":checked")) {
                        return true;
                    } else {
                        return !utils.isEmpty(value);
                    }
            },            $.mage.__('This is a required field.')
        );        validator.addRule(
            'validate-create_password',
            function (value) {
                if(hiddenLoginFields.is(':visible') || !$('input[name*="create_account_checkbox"]').is(":checked")) {
                    return true;
                } else {
                    var pass;

                    if (value == null) {
                        return false;
                    }

                    pass = $.trim(value);

                    if (!pass.length) {
                        return true;
                    }

                    return !(pass.length > 0 && pass.length < 6);
                }
            },            $.mage.__('Please enter 6 or more characters. Leading and trailing spaces will be ignored.')
        );        validator.addRule(
            'validate-confirm_password',
            function (value) {
                if(hiddenLoginFields.is(':visible') || !$('input[name*="create_account_checkbox"]').is(":checked")) {
                    return true;
                } else {
                    return $('input[name*="create_account_password"]').val() === $('input[name*="create_account_password_confirm"]').val();
                }
            },            $.mage.__('Please enter the same value again.')
        );

 

 

Re: How can I validate my custom checkbox on LayoutProcessor.php

Thank you so much for your response!

 

And I put the validator on : Magento/module_checkout/Model/frontend/web/js/model/default-validator.js ?

 

Thank you:

Re: How can I validate my custom checkbox on LayoutProcessor.php

I'm sorry but I'm still lost Smiley Sad

 

I do not know what to do exactly after adding my newsletter checkbox in the LayoutPorcessor.php to receive the customer's email in the newsletter table. 

 

I need to validate my code in LayoutPorcessor.php

 

            if (isset($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
                ['shippingAddress']['children']['shipping-address-fieldset']['children'])
            ) {
                $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
                ['shippingAddress']['children']['shipping-address-fieldset']['children']['subscribe'] = [
                    'component' => 'Magento_Ui/js/form/element/abstract',
                    'config' => [
                        'customScope' => 'shippingAddress',
                        'template' => 'ui/form/field',
                        'elementTmpl' => 'ui/form/element/checkbox',
                        'options' => [],
                        'id' => 'delivery-date',
                    ],
                    'dataScope' => 'shippingAddress.newsletter_subscribe',
                    'label' => 'Sign Up for Newsletter',
                    'provider' => 'checkoutProvider',
                    'visible' => true,
                    'checked' => true,
                    'validation' => [],
                    'sortOrder' => 250,
                    'id' => 'create_account_checkbox',
                    'value' => 'create_account_checkbox',
                ];
            }


        return $jsLayout;
    }

With default-validator.js ? After I need to call subscriber model in the checkout module controller to get the email in newsletter table after finishing the checkout process  ?