FormKit is a lightweight and flexible SwiftUI form framework designed for declarative, real-time, and user-friendly form validation. It supports per-field validation, error display, touched state tracking, and reactive form validity with a focus on SwiftUI best practices.
- ✅ Field-level validation (required, min length, custom rules)
- ✅ Live validation on user interaction
- ✅ Reactive
form.isValid
for controlling submit buttons - ✅ Clean separation of concerns using
FormModel
andFormField
- ✅ Touched-based error visibility (errors show only after interaction)
- ✅ Full SwiftUI compatibility (iOS 15+)
- ✅ Demo app with Login and Registration flows
@StateObject private var form = FormModel {
("email", FormField(value: "", validators: [
.required(message: "Email is required"),
.custom { $0.contains("@") ? nil : "Invalid email" }
]))
}
TextField("Email", text: form.binding(for: "email"))
.onChange(of: form.binding(for: "email").wrappedValue) {
form.validate(field: "email")
}
if let error = form.error(for: "email"), form.touched(for: "email") {
Text(error).foregroundColor(.red)
}
Button("Submit") {
form.markAllTouched()
}
.disabled(!form.isValid)
The demo includes:
- ✉️ Login form: email + password validation
- 📝 Registration form: name, age, optional bio
- 🗭 Navigation between login and registration
- 🎨 Custom border styling without UIKit
TextFieldStyle
Start with:
HomeView()
To navigate between both forms.
.required(message: String)
.minLength(Int, message: String)
.custom((Value) -> String?) // nil means valid
More validators like .emailFormat
, .matchesField
, .numericOnly
, etc. can be added easily.
- Async validation support
- Reusable
ValidatedTextField
SwiftUI view - Form reset / initial values
- Validation groups and conditional rules
- Swift Package release
FormKit/
├─ Sources/
│ ├─ FormModel.swift
│ ├─ FormField.swift
│ ├─ Validator.swift
│ ├─ Protocols.swift
│ └─ FormBuilder.swift
├─ Demo/
│ ├─ HomeView.swift
│ ├─ LoginView.swift
│ ├─ RegistrationView.swift
│ └─ SharedUI.swift (validatedField)
- iOS 15+
- Swift 5.7+
- Xcode 14+
Found a bug or want to help extend validation? Open a PR or create an issue. Feedback is welcome!
MIT License. Use it, fork it, ship it 🚀