Is it possible to have a TextField that validates its input on every character entered by the user so that the string displayed by the text field updates only on successful validation?
The documentation on the TextField seems to suggest that this is possible by using a bound value. In my testing this is not the case. Even when validation fails, the text field always updates the string displayed.
Changes to the bound value update the string displayed by the text field. Editing the text field updates the bound value, as long as the formatter can parse the text. If the format style can’t parse the input, the bound value remains unchanged.
https://developer.apple.com/documentation/swiftui/textfield/init(_:value:formatter:)-9jw0i
The way I understand it, in case of an error thrown by ParseStrategy/parse(_ value: String) the bound value remains unchanged. Since the bound value did not change the string displayed by the text field should not be updated.
I have tried the code example used in the documentation overview of the TextField to create a sample app.
@State private var nameComponents = PersonNameComponents()
var body: some View {
TextField(
"Proper name",
value: $nameComponents,
format: .name(style: .medium)
)
.onSubmit {
validate(components: nameComponents)
}
.disableAutocorrection(true)
.border(.secondary)
Text(nameComponents.debugDescription)
}
The bound value doesn’t have to be a string. By using a FormatStyle, you can bind the text field to a nonstring type, using the format style to convert the typed text into an instance of the bound type. The following example uses a PersonNameComponents.FormatStyle to convert the name typed in the text field to a PersonNameComponents instance. A Text view below the text field shows the debug description string of this instance.
https://developer.apple.com/documentation/swiftui/textfield
Topic:
UI Frameworks
SubTopic:
SwiftUI