0

Here in new Billing address form in checkout page I have fields of Zip/Postal Code, and Phone number. I added Validation for those two working fine. But its getting saved if they are Empty. How to make the form should not submit if they are Empty.

MyVendor/MyModule/view/frontend/layout/checkout_index_index.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="checkout" 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="shipping-step" xsi:type="array">
                                                <item name="children" xsi:type="array">
                                                    <item name="shippingAddress" xsi:type="array">
                                                        <item name="children" xsi:type="array">
                                                            <item name="shipping-address-fieldset" xsi:type="array">
                                                                <item name="children" xsi:type="array">
                                                                    <item name="firstname" xsi:type="array">
                                                                        <item name="validation" xsi:type="array">
                                                                            <item name="letters-only" xsi:type="boolean">true</item>
                                                                            <item name="required-entry" xsi:type="boolean">true</item>
                                                                            <item name="no-whitespace" xsi:type="boolean">false</item>                    
                                                                        </item>
                                                                    </item>
                                                                    <item name="lastname" xsi:type="array">
                                                                        <item name="validation" xsi:type="array">
                                                                            <item name="letters-only" xsi:type="boolean">true</item>
                                                                            <item name="required-entry" xsi:type="boolean">true</item>
                                                                            <item name="no-whitespace" xsi:type="boolean">false</item>
                                                                        </item>
                                                                    </item>
                                                                    <item name="postcode" xsi:type="array">
                                                                        <item name="validation" xsi:type="array">
                                                                            <item name="required-entry" xsi:type="boolean">false</item>
                                                                            <item name="custom_pincode_validation" xsi:type="string">true</item>
                                                                        </item>
                                                                         <item name="sortOrder" xsi:type="string">100</item>
                                                                    </item>
                                                                    <item name="city" xsi:type="array">
                                                                        <item name="sortOrder" xsi:type="string">101</item>
                                                                    </item>

                                                                    <item name="region_id" xsi:type="array">
                                                                        <item name="sortOrder" xsi:type="string">102</item>
                                                                    </item>

                                                                    <item name="company" xsi:type="array">...</item>
                                                                    <item name="fax" xsi:type="array">...</item>
                                                                    <item name="country_id" xsi:type="array">...</item>
                                                                    <item name="telephone" xsi:type="array">
                                                                        <item name="validation" xsi:type="array">
                                                                            <item name="required-entry" xsi:type="boolean">true</item>
                                                                            <item name="mobileIN" xsi:type="string">true</item>
                                                                        </item>
                                                                    </item>
                                                                </item>
                                                            </item>
                                                        </item>
                                                    </item>
                                                </item>
                                            </item>
                                        </item>
                                    </item>
                                </item>
                            </item>
                        </item>
                    </argument>
                </arguments>
            </referenceBlock>
    </body>
</page>

Here is the Validation file

    app/code/MyVendor/BillingInfoForm/Plugin/Block/Checkout/LayoutProcessor.php

<?php
namespace MyVendor\BillingInfoForm\Plugin\Block\Checkout;
use Magento\Checkout\Block\Checkout\LayoutProcessor as MageLayoutProcessor;

class LayoutProcessor
{
    public function afterProcess(MageLayoutProcessor $subject, $jsLayout)
    {


        /* config: checkout/options/display_billing_address_on = payment_method */
        if (isset($jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
            ['payment']['children']['payments-list']['children']
        )) {

            foreach ($jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
                     ['payment']['children']['payments-list']['children'] as $key => $payment) {

                /* postcode */
                if (isset($payment['children']['form-fields']['children']['postcode'])) {

                    $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
                    ['payment']['children']['payments-list']['children'][$key]['children']['form-fields']['children']
                    ['postcode'] =  [
                        'component' => 'Magento_Ui/js/form/element/post-code',
                        'config' => [
                            'customScope' => 'billingingAddress',
                            'template' => 'ui/form/field',
                            'elementTmpl' => 'ui/form/element/input',
                            'options' => [],
                            'id' => 'postcode'
                        ],
                        'dataScope' => 'billingingAddress.postcode',
                        'label' => 'Zip/Postal Code',
                        'provider' => 'checkoutProvider',
                        'visible' => true,
                        'validation' =>["required-entry" => true, "min_text_length" =>6,
                        "max_text_length" =>6,"no-whitespace"=> true,"validate-number"=>true],
                        'sortOrder' => 100,
                        'id' => 'postcode'
                    ];
                }

                /* city */
                if (isset($payment['children']['form-fields']['children']['city'])) {

                    $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
                    ['payment']['children']['payments-list']['children'][$key]['children']['form-fields']['children']
                    ['city']['sortOrder'] = 101;


                }
                if (isset($payment['children']['form-fields']['children']['region_id'])) {

                    $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
                    ['payment']['children']['payments-list']['children'][$key]['children']['form-fields']['children']
                    ['region_id']['sortOrder'] = 102;

                }
                 if (isset($payment['children']['form-fields']['children']['country_id'])) {

                    $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
                    ['payment']['children']['payments-list']['children'][$key]['children']['form-fields']['children']
                    ['country_id']['sortOrder'] = 103;

                }

                /* telephone */
                if (isset($payment['children']['form-fields']['children']['telephone'])) {

                $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
                ['payment']['children']['payments-list']['children'][$key]['children']['form-fields']['children']
                ['telephone'] =  [
                'component' => 'Magento_Ui/js/form/element/abstract',
                'config' => [
                    'customScope' => 'billingingAddress',
                    'template' => 'ui/form/field',
                    'elementTmpl' => 'ui/form/element/input',
                    'options' => [],
                    'id' => 'telephone'
                ],
                'dataScope' => 'billingingAddress.telephone',
                'label' => 'Phone Number',
                'provider' => 'checkoutProvider',
                'visible' => true,
                'validation' =>['required-entry' => true, "min_text_length" => 10,
                "max_text_length" => 10,"no-whitespace"=> true,"validate-number"=>true],
                'sortOrder' => 104,
                'id' => 'telephone'
            ];

            }   
        }

        return $jsLayout;
    }
  }
}

Can I get help?. Thank you in advance

0

The attribute validation check is based on the bool value "is_required" in table "Eav Attribute".

Set the attribute is_required = '1' for mandate fields Zip/Postal Code & Phone number

UPDATE `eav_attribute` SET `is_required` = '1' WHERE `eav_attribute`.`attribute_code` = 'phone_number';
| improve this answer | |
  • For phone_number is_required is already 1, and i update for postcode but still it saving with empty value. – Praveen Negimani Oct 25 '19 at 7:13
  • Did u clear the cache. php bin/magento cache:flush or php bin/magento cache:clean – Bala Varadarajan Oct 25 '19 at 7:20
  • Thank you for responce @Bala Varadarajan, Yes I DId cache:flush and cache:clear, but still not working. – Praveen Negimani Oct 25 '19 at 7:33

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.