From 573e56d5c0bb31b876a69468be643822725cca58 Mon Sep 17 00:00:00 2001 From: Nadir Seghir Date: Fri, 5 May 2023 13:30:21 +0100 Subject: [PATCH 1/2] Only validate postcode if its required --- .../cart-checkout/address-form/address-form.tsx | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/assets/js/base/components/cart-checkout/address-form/address-form.tsx b/assets/js/base/components/cart-checkout/address-form/address-form.tsx index def20c9943a..e06f3d3afa2 100644 --- a/assets/js/base/components/cart-checkout/address-form/address-form.tsx +++ b/assets/js/base/components/cart-checkout/address-form/address-form.tsx @@ -265,11 +265,13 @@ const AddressForm = ( { } ) } customValidation={ ( inputObject: HTMLInputElement ) => - customValidationHandler( - inputObject, - field.key, - values - ) + field.required || inputObject.value + ? customValidationHandler( + inputObject, + field.key, + values + ) + : true } errorMessage={ field.errorMessage } required={ field.required } From 911f94c3549272052fde943dc3bf99269a796a63 Mon Sep 17 00:00:00 2001 From: Niels Lange Date: Tue, 9 May 2023 16:19:46 +0700 Subject: [PATCH 2/2] Clear postcode validation error if postcode is not required --- .../address-form/address-form.tsx | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/assets/js/base/components/cart-checkout/address-form/address-form.tsx b/assets/js/base/components/cart-checkout/address-form/address-form.tsx index e06f3d3afa2..28fb22b0a27 100644 --- a/assets/js/base/components/cart-checkout/address-form/address-form.tsx +++ b/assets/js/base/components/cart-checkout/address-form/address-form.tsx @@ -21,7 +21,7 @@ import { defaultAddressFields, ShippingAddress, } from '@woocommerce/settings'; -import { useSelect, useDispatch } from '@wordpress/data'; +import { useSelect, useDispatch, dispatch } from '@wordpress/data'; import { VALIDATION_STORE_KEY } from '@woocommerce/block-data'; import { FieldValidationStatus } from '@woocommerce/types'; @@ -123,6 +123,23 @@ const AddressForm = ( { } ); }, [ addressFormFields, onChange, values ] ); + // Clear postcode validation error if postcode is not required. + useEffect( () => { + addressFormFields.forEach( ( field ) => { + if ( field.key === 'postcode' && field.required === false ) { + const store = dispatch( 'wc/store/validation' ); + + if ( type === 'shipping' ) { + store.clearValidationError( 'shipping_postcode' ); + } + + if ( type === 'billing' ) { + store.clearValidationError( 'billing_postcode' ); + } + } + } ); + }, [ addressFormFields, type, clearValidationError ] ); + useEffect( () => { if ( type === 'shipping' ) { validateShippingCountry(