Learn how to configure the postcode component.
The postcode component captures postal or ZIP codes. At minimum, you can create it with a label:
import SwiftUI
import PXPCheckoutSDK
let config = PostcodeComponentConfig(
label: "Postcode",
placeholder: "Enter postcode"
)
let postcode = try pxpCheckout.create(.postcode, componentConfig: config) as! PostcodeComponent
postcode.buildContent()The postcode field allows alphanumeric characters, spaces, and hyphens. Other characters are stripped. Values are trimmed and capped at 10 characters.
You can customise field labels, validation messages, and styling:
var config = PostcodeComponentConfig(
label: "ZIP / Postcode",
placeholder: "e.g. SW1A 1AA",
guideText: "Use the format for your region",
validations: PostcodeValidationResource(
PC01: "Enter a postcode",
PC02: "Postcode is too long"
),
onValidationFailed: { results in }
)
config.componentStyles = FieldStyle(cornerRadius: 8)
config.displayValidIcon = true
config.validationOnChange = true
let postcode = try pxpCheckout.create(.postcode, componentConfig: config) as! PostcodeComponentYou can customise validation messages and field behaviour:
| Property | Description |
|---|---|
validationsPostcodeValidationResource? | The custom validation error messages. Overrides SDK default messages for PC01 and PC02 error codes. Defaults to SDK localisation. |
labelString? | The field label text. Defaults to nil. |
placeholderString? | The placeholder text shown when the field is empty. Defaults to nil. |
guideTextString? | The helper text displayed below the field. Defaults to nil. |
accessibilityLabelString? | The VoiceOver accessibility label. Defaults to nil. |
isRequiredBool | Whether the field is required. For the postcode component, this is always true and can't be changed. Validation fails if the field is empty. |
componentStylesFieldStyle? | The container styling. Defaults to nil. Properties: color, font, fontWeight, fontSize, labelFont, labelColor, valueFont, valueColor, backgroundColor, borderColor, borderWidth, cornerRadius, padding, margin, textAlignment, opacity, shadow (ShadowStyle), icon (IconStyle), labelSpacing. |
inputStylesFieldInputStateStyles? | The input styling for different states. Defaults to nil. States: base (default unfocused), active (focused), valid (has valid value), invalid (has invalid value). Each state supports: color, placeholderColor, fontWeight, fontSize, backgroundColor, borderColor, borderWidth, cornerRadius, padding, minHeight. |
labelStylesFieldLabelStateStyles? | The label styling by state. Defaults to nil. States: base (default), active (focused), valid (has valid value), invalid (has invalid value). Each state supports: color, font. Additional property: minWidth (CGFloat? - default: 80). |
labelPositionLabelPosition? | The label position relative to input. Options: above, below, left, right. Defaults to nil. |
invalidTextStyleFieldMessageStyle? | The error text styling. Defaults to nil. Properties: color, font. |
invalidTextPositionTextPosition? | The error message placement. Defaults to nil. |
guideTextStyleFieldMessageStyle? | The guide text styling. Defaults to nil. Properties: color, font. |
invalidIconImage? | The custom error icon. Defaults to nil. |
validIconImage? | The custom valid icon. Defaults to nil. |
displayValidIconBool | Whether to display the valid icon. Defaults to false. |
displayInvalidIconBool | Whether to display the error icon. Defaults to true. |
tooltipAccessibilityLabelString? | The tooltip accessibility label. Defaults to nil. |
displayRequiredIconBool? | Whether to display the required indicator. Defaults to nil. |
allowToCopyPasteBool | Whether to allow copy and paste. Defaults to true. |
validationOnBlurBool | Whether to validate when the field loses focus. Defaults to true. |
validationOnChangeBool | Whether to validate as the user types. Defaults to false. |
isDisabledBool | Whether the field is disabled. Defaults to false. |
You can override default validation error messages:
| Property | Description |
|---|---|
PC01String | Required. The message displayed when the field is empty but required. |
PC02String | Required. The message displayed when the postcode exceeds 10 characters. |
Configure callbacks to respond to field events:
| Property | Description |
|---|---|
onChange(() -> Void)? | Called when the postcode value changes. Triggered as the user types or when the value is set programmatically. Defaults to nil. |
onFocus(() -> Void)? | Called when the field gains focus. Triggered when the user taps into the field. Defaults to nil. |
onBlur(() -> Void)? | Called when the field loses focus. Triggered when the user taps outside the field or moves to another field. Defaults to nil. |
onValidationPassed(([ValidationResult]) -> Void)? | Called when validation passes. Receives an array of validation results. Defaults to nil. |
onValidationFailed(([ValidationResult]) -> Void)? | Called when validation fails. Receives an array of validation results containing error details. Defaults to nil. |
For more information about event patterns, see Events.
The postcode field validates input against the following rules:
| Code | Condition | Default message |
|---|---|---|
| PC01 | The field is empty but required | "Please enter postcode" |
| PC02 | The postcode exceeds 10 characters | "The postcode exceeded maximum length" |
The SDK automatically strips non-alphanumeric characters (except spaces and hyphens), trims whitespace, and enforces the 10-character maximum. Validation triggers based on your configuration (validationOnBlur, validationOnChange). Override default messages using the validations property.
Returns SwiftUI content for the postcode field:
postcode.buildContent()A simple postcode field with default settings:
let config = PostcodeComponentConfig(label: "Postcode")
let postcode = try pxpCheckout.create(.postcode, componentConfig: config) as! PostcodeComponentApply custom styling to the field:
var config = PostcodeComponentConfig(label: "Postal code")
config.inputStyles = FieldInputStateStyles(
base: FieldInputStyle(
cornerRadius: 6,
borderWidth: 1,
borderColor: Color(.separator)
),
active: FieldInputStyle(
borderColor: .accentColor,
borderWidth: 2
)
)
config.displayValidIcon = true
let postcode = try pxpCheckout.create(.postcode, componentConfig: config) as! PostcodeComponentHandle validation events:
var config = PostcodeComponentConfig(label: "Postcode")
config.onValidationPassed = { results in
print("Postcode is valid")
}
config.onValidationFailed = { results in
print("Postcode validation failed")
}
config.validationOnChange = true
let postcode = try pxpCheckout.create(.postcode, componentConfig: config) as! PostcodeComponentOverride default validation messages:
let config = PostcodeComponentConfig(
label: "Postcode",
validations: PostcodeValidationResource(
PC01: "Please enter your postcode",
PC02: "Postcode must be 10 characters or less"
)
)
let postcode = try pxpCheckout.create(.postcode, componentConfig: config) as! PostcodeComponentThe postcode component is typically used inside the billing address component:
var postcodeConfig = PostcodeComponentConfig(
label: "Postcode",
placeholder: "Enter postcode"
)
let config = BillingAddressComponentConfig(
fields: BillingAddressComponentFields(
postcode: postcodeConfig
)
)
let billing = try pxpCheckout.create(.billingAddress, componentConfig: config) as! BillingAddressComponentThe postcode field works with the pre-fill billing address checkbox. When the checkbox is enabled, it copies the shipping postcode into this field:
let checkoutConfig = CheckoutConfig(
environment: .test,
session: sessionData,
transactionData: transactionData,
merchantShopperId: "shopper-1",
ownerId: "owner-id",
onGetShippingAddress: {
ShippingAddress(
countryCode: "GB",
postalCode: "SW1A 1AA",
address: "10 Downing Street"
)
}
)
let pxpCheckout = try PxpCheckout.initialize(config: checkoutConfig)
let billing = try pxpCheckout.create(.billingAddress, componentConfig: BillingAddressComponentConfig()) as! BillingAddressComponentFor billing address integration, see Billing address and Pre-fill billing address checkbox. For AVS and submit workflows, see Card submit.