SwiftUI Combine ViewModel Email Validation



我如何验证电子邮件内部视图模型结合这两个代码使用组合?

这是在我的RegisterViewModel

private var isEmailValidPublisher: ValidatePublisher {
$email
.removeDuplicates()
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines).count >= 10}
.handleEvents(receiveOutput: { [weak self] in $0 ? (self?.emailMessage = "") : (self?.emailMessage = "Invalid email")})
.eraseToAnyPublisher()
}

我想在isEmailValidPublisher中添加这个验证。

func isValidEmail() -> Bool {
let emailRegEx = "(?:[\p{L}0-9!#$%\&'*+/=?\^_`{|}~-]+(?:\.[\p{L}0-9!#$%\&'*+/=?\^_`{|}" + "~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\" + "x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[\p{L}0-9](?:[a-" + "z0-9-]*[\p{L}0-9])?\.)+[\p{L}0-9](?:[\p{L}0-9-]*[\p{L}0-9])?|\[(?:(?:25[0-5" + "]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-" + "9][0-9]?|[\p{L}0-9-]*[\p{L}0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21" + "-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])"

let emailValidation = NSPredicate(format:"SELF MATCHES[c] %@", emailRegEx)
return emailValidation.evaluate(with: self)
}

我如何在isEmailValidPublisher中混合它们,以便我可以在我的signupview中使用这些信息,就像下面

Image(systemName: "envelope")
.foregroundColor(registerVM.emailMessage != "" ? .red: .blue)

假设你有一个这样的简单视图:

struct ContentView: View {

@StateObject var controller = Controller()

var body: some View {
VStack {
TextField("email", text: $controller.email)
.foregroundColor(controller.inputValid ? .primary : .red)
controller.validationMessage.map { message in
Text(message)
.foregroundColor(.red)
}
}
}
}

,你想在你的ObservableObject中有逻辑:

class Controller: ObservableObject {

@Published var email = ""
@Published private(set) var validationMessage: String?

var inputValid: Bool {
validationMessage == nil
}


init() {
// you subscribe to any changes to the email field input
$email
// you ignore the first empty value that it gets initialised with
.dropFirst()
// you give the user a bit of time to finish typing
.debounce(for: 0.6, scheduler: RunLoop.main)
// you get rid of duplicated inputs as they do not change anything in terms of validation
.removeDuplicates()
// you validate the input string and in case of problems publish an error message 
.map { input in
guard !input.isEmpty else {
return "Email cannot be left empty"
}
guard input.isValidEmail() else {
return "Email is not valid"
}
// in case the input is valid the error message is nil
return nil
}
// you publish the error message for the view to react to
.assign(to: &$validationMessage)
}
}

相关内容

  • 没有找到相关文章

最新更新