Skip to content

Commit 7b95406

Browse files
committed
✨Implement simple form validation
1 parent 37c8dfb commit 7b95406

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

SwiftUI-Combine/ContentView.swift

+14-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ class UserModel: ObservableObject {
1414
@Published var password = ""
1515
@Published var passwordAgain = ""
1616
@Published var valid = false
17+
18+
private var cancellableSet: Set<AnyCancellable> = []
19+
20+
init() {
21+
$userName
22+
.debounce(for: 0.8, scheduler: RunLoop.main)
23+
.removeDuplicates()
24+
.map { input in
25+
return input.count >= 3
26+
}
27+
.assign(to: \.valid, on: self)
28+
.store(in: &cancellableSet)
29+
}
1730
}
1831

1932
struct ContentView: View {
@@ -39,7 +52,7 @@ struct ContentView: View {
3952
}
4053

4154
}
42-
55+
4356
struct ContentView_Previews: PreviewProvider {
4457
static var previews: some View {
4558
ContentView()

0 commit comments

Comments
 (0)