Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit 344f5b4

Browse files
senadirnielslange
andauthored
Only validate postcode if its required or filled (#9377)
* Only validate postcode if its required * Clear postcode validation error if postcode is not required --------- Co-authored-by: Niels Lange <[email protected]>
1 parent b0a4fc4 commit 344f5b4

1 file changed

Lines changed: 25 additions & 6 deletions

File tree

assets/js/base/components/cart-checkout/address-form/address-form.tsx

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
defaultAddressFields,
2222
ShippingAddress,
2323
} from '@woocommerce/settings';
24-
import { useSelect, useDispatch } from '@wordpress/data';
24+
import { useSelect, useDispatch, dispatch } from '@wordpress/data';
2525
import { VALIDATION_STORE_KEY } from '@woocommerce/block-data';
2626
import { FieldValidationStatus } from '@woocommerce/types';
2727

@@ -123,6 +123,23 @@ const AddressForm = ( {
123123
} );
124124
}, [ addressFormFields, onChange, values ] );
125125

126+
// Clear postcode validation error if postcode is not required.
127+
useEffect( () => {
128+
addressFormFields.forEach( ( field ) => {
129+
if ( field.key === 'postcode' && field.required === false ) {
130+
const store = dispatch( 'wc/store/validation' );
131+
132+
if ( type === 'shipping' ) {
133+
store.clearValidationError( 'shipping_postcode' );
134+
}
135+
136+
if ( type === 'billing' ) {
137+
store.clearValidationError( 'billing_postcode' );
138+
}
139+
}
140+
} );
141+
}, [ addressFormFields, type, clearValidationError ] );
142+
126143
useEffect( () => {
127144
if ( type === 'shipping' ) {
128145
validateShippingCountry(
@@ -265,11 +282,13 @@ const AddressForm = ( {
265282
} )
266283
}
267284
customValidation={ ( inputObject: HTMLInputElement ) =>
268-
customValidationHandler(
269-
inputObject,
270-
field.key,
271-
values
272-
)
285+
field.required || inputObject.value
286+
? customValidationHandler(
287+
inputObject,
288+
field.key,
289+
values
290+
)
291+
: true
273292
}
274293
errorMessage={ field.errorMessage }
275294
required={ field.required }

0 commit comments

Comments
 (0)