mohsinbmwm3/formkit
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.
π Features
- β Field-level validation (required, min length, custom rules)
- β Live validation on user interaction
- β
Reactive
form.isValidfor controlling submit buttons - β
Clean separation of concerns using
FormModelandFormField - β Touched-based error visibility (errors show only after interaction)
- β Full SwiftUI compatibility (iOS 15+)
- β Demo app with Login and Registration flows
π§ How It Works
1. Define a Form
@StateObject private var form = FormModel {
("email", FormField(value: "", validators: [
.required(message: "Email is required"),
.custom { $0.contains("@") ? nil : "Invalid email" }
]))
}2. Bind and Validate
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)
}3. Check Overall Validity
Button("Submit") {
form.markAllTouched()
}
.disabled(!form.isValid)π± Demo App
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.
π§Ή Validator Types
.required(message: String)
.minLength(Int, message: String)
.custom((Value) -> String?) // nil means validMore validators like .emailFormat, .matchesField, .numericOnly, etc. can be added easily.
π§ͺ Future Plans
- [ ] Async validation support
- [ ] Reusable
ValidatedTextFieldSwiftUI view - [ ] Form reset / initial values
- [ ] Validation groups and conditional rules
- [ ] Swift Package release
π Folder Structure
FormKit/
ββ Sources/
β ββ FormModel.swift
β ββ FormField.swift
β ββ Validator.swift
β ββ Protocols.swift
β ββ FormBuilder.swift
ββ Demo/
β ββ HomeView.swift
β ββ LoginView.swift
β ββ RegistrationView.swift
β ββ SharedUI.swift (validatedField)π¦ Requirements
- iOS 15+
- Swift 5.7+
- Xcode 14+
π₯ Contributing
Found a bug or want to help extend validation? Open a PR or create an issue. Feedback is welcome!
π License
MIT License. Use it, fork it, ship it π
Package Metadata
Repository: mohsinbmwm3/formkit
Default branch: main
README: README.md