Lightweight library for grouping input fields.
Each field has a set of rules to be validated. On form validation all fields gets validated one-by-one. Failed rules for each field passed to field itself, to show error, for example.
- Input field fully customizable.
- Shift focus to next field on return.
- Shif focus to first failed field of form validation.
- Validate form with custom behaviour.
- Display/Hide error for every input field with custom behaviour.
- A banch of predefined text validation rules for most cases.
- Custom validation rules.
- Create input field and display error for failed validation rules, if needed.
struct MyField: View {
let title: String
let text: Binding<String>
// Failed rules come from FormField during whole form validation.
let failedRules: [TextValidationRule]
var body: some View {
VStack(alignment: .leading) {
TextField(title, text: text)
.background(Color.white)
// Display error.
if failedRules.isEmpty == false {
// Show error for first failed rule.
Text(failedRules[0].errorMessage)
.foregroundColor(.red)
}
}
}
}
- Group input fields into form.
struct ContentView: View {
@State var name: String = ""
var body: some View {
FormView( First failed field
validate: .never, // Form will be validated on user action.
hideError: .onValueChanged // Error for field wil be hidden on field value change.
) { proxy in
FormField(
value: $name,
rules: [TextValidationRule.notEmpty(message: "Name field should no be empty")]
) { failedRules in
MyField(title: "Name", text: $name, failedRules: failedRules)
}
// Other input fields...
Button("Validate") {
// Validate form on user action.
print("Form is valid: \(proxy.validate())")
}
}
}
}
Form validated at one of three specific times:
onFieldValueChanged
- each field validated on it's value changed.onFieldFocusLost
- each field validated on focus lost.never
- on callproxy.validate()
. Default behaviour. First failed field is focused automatically.
Error for each field gets hidden at one of three specific times:
onValueChanged
- value of field with error has changed. Defaule behaviour.onFocus
- field with error is focused..onFucusLost
- field with error lost focus.
One of two ways:
- Adopt protocol
ValidationRule
:
public protocol ValidationRule {
associatedtype Value
func check(value: Value) -> Bool
}
- Extend
TextValidationRule
:
extension TextValidationRule {
static var myRule: Self {
TextValidationRule(message: "Text should not be empty") { value in
value.isEmpty == false
}
}
}
A banch of predefind rules for text validation is available via TextValidationRule
:
- notEmpty - value not empty.
- digitsOnly - value contains only digits.
- lettersOnly - value contains only letters.
- email - is valid email.
- minLenght/maxLenth - value length greate/less.
- regex - evaluate regular expresstion.
- equalTo - value equal to another value. Useful for password confirmation.
- etc...
FormView doesn't use any external dependencies.
- Swift 5.0
- iOS 15.0
dependencies: [
.package(
url: "https://github.com/MobileUpLLC/FormView",
.upToNextMajor(from: "1.1.2")
)
]
FormView is destributed under the MIT license.