Learn how to configure the cardholder name component.
The cardholder name component collects the name on the card. At minimum, you can create it with a label:
import SwiftUI
import PXPCheckoutSDK
var config = CardHolderNameComponentConfig(
label: "Name on card",
placeholder: "J SMITH"
)
config.isRequired = true
let holderName = try pxpCheckout.create(.cardHolderName, componentConfig: config) as! CardHolderNameComponent
holderName.buildContent()You can customise auto-capitalization, validation messages, hint icons, and styling:
var config = CardHolderNameComponentConfig(
label: "Cardholder name",
placeholder: "As shown on card",
guideText: "Use the name printed on your card",
autoCapitalise: true,
defaultValue: nil,
validations: CardHolderNameValidationResource(
HN01: "Enter the cardholder name",
HN02: "Name is too short",
HN03: "Use letters, spaces, or hyphens only",
HN04: "Name is too long"
),
showHintIcon: true,
showTooltip: true,
validationOnBlur: true,
validationOnChange: false
)
config.isRequired = true
config.componentStyles = FieldStyle(cornerRadius: 8)
config.displayValidIcon = true
let holderName = try pxpCheckout.create(.cardHolderName, componentConfig: config) as! CardHolderNameComponent| Property | Description |
|---|---|
autoCapitaliseBool | Whether to automatically capitalise the first letter of each word (title case). When true, text is converted to title case as the user types (for example "john smith" becomes "John Smith"). Defaults to true. |
defaultValueString? | The pre-filled display value. When set, the field initially shows this cardholder name. Defaults to nil. |
validationsCardHolderNameValidationResource? | Custom validation messages for HN01-HN04 error codes. Overrides SDK default messages. Defaults to nil. |
shouldEncryptBool | Whether to encrypt the value when read. When true, the cardholder name is encrypted before transmission. Defaults to false. |
showHintIconBool | Whether to show a hint icon beside the field. Helps users understand what name to enter. Defaults to false. |
showTooltipBool | Whether to show tooltip behaviour with the hint. When true, displays additional information when the hint icon is tapped. Defaults to false. |
hintIconImage? | The custom hint icon image. Defaults to nil. |
tooltipIconImage? | The custom tooltip trigger image. Defaults to nil. |
maskHiddenIconImage? | The icon when value is masked. Defaults to nil. |
maskVisibleIconImage? | The icon when value is visible. Defaults to nil. |
onPaste(() -> Void)? | Event handler called when user pastes into the field. Defaults to nil. |
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 accessibility label for the input. Defaults to nil. |
isRequiredBool | Whether the field is required. When true, the field must have a value before the payment can be submitted. Defaults to false. |
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 message 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 invalid state icon. Defaults to nil. |
validIconImage? | The custom valid state icon. Defaults to nil. |
displayValidIconBool | Whether to show the valid icon. Defaults to false. |
displayInvalidIconBool | Whether to show the invalid 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 on each change. Defaults to false. |
isDisabledBool | Whether the field is disabled. Defaults to false. |
The cardholder name component provides event handlers for user interaction and validation:
| Callback | Description |
|---|---|
onChange: (() -> Void)? | Called when the field value changes. |
onFocus: (() -> Void)? | Called when the field gains focus. |
onBlur: (() -> Void)? | Called when the field loses focus. |
onValidationPassed: (([ValidationResult]) -> Void)? | Called when all validation checks pass. Receives an array of validation results. |
onValidationFailed: (([ValidationResult]) -> Void)? | Called when validation fails. Receives an array of validation results containing error details. |
onPaste: (() -> Void)? | Called when user pastes into the field. |
For more information about event patterns, see Events.
The cardholder name component validates user input against the following rules:
| Code | Condition | Default message |
|---|---|---|
| HN01 | Required but empty | "Please enter card holder name" |
| HN02 | Fewer than 3 characters | "Card holder name minimum length is 3" |
| HN03 | Contains invalid characters | "The card holder name must be string containing only alphabetic characters, spaces and hyphens" |
| HN04 | More than 100 characters | "Card holder name maximum length is 100" |
Validation triggers based on your configuration (validationOnBlur, validationOnChange). Override default messages using the validations property. Only letters, spaces, and hyphens are allowed.
Returns SwiftUI content for the cardholder name field:
holderName.buildContent()Prefill the name and disable automatic title case conversion:
var config = CardHolderNameComponentConfig(
label: "Name on card",
defaultValue: "Jane Doe",
autoCapitalise: false
)
config.isRequired = true
let holderName = try pxpCheckout.create(.cardHolderName, componentConfig: config) as! CardHolderNameComponentCreate an optional cardholder name field:
let config = CardHolderNameComponentConfig(label: "Name (optional)")
let holderName = try pxpCheckout.create(.cardHolderName, componentConfig: config) as! CardHolderNameComponentPass the component into Card submit or New card. For validation details, see Data validation.