Given a TextField which should receive a phone, format the number with XX-XX-XXXX pattern, and this specific input field should not allow input over 8 symbols, and only alphanumeric and so on. I'm trying to use @ObservableObject with @Published var, however, when the value is updated with suffix, the text input position does not jump to the end of the input field:
class SignupModel: ObservableObject {
@Published var phoneNumber: String = "" {
didSet {
if (self.phoneNumber.count == 3 || self.phoneNumber.count == 5) {
if (self.phoneNumber.count == 3) {
self.phoneNumber.insert("-", at: String.Index(encodedOffset: 3))
} else if self.phoneNumber.count == 5 {
self.phoneNumber.insert("-", at: String.Index(encodedOffset: 5))
}
}
print(self.phoneNumber)
}
}
}
...
@ObservedObject var formModel: SignupModel = SignupModel()
...
TextField("12 3333 123", text: self.$formModel.phoneNumber)
What would be the valid approach to achieve validation and formatting using combine?