SwiftUI Package supporting "Form Validation"
Take a look to Build field validator for SwiftUI and in folder Example for a reference implementation
This Library is compatible with Swift Package Manager
This Library is compatible with Cocoapods.
In your Podfile add
pod 'FieldValidatorLibrary', '~> 1.5.0'
struct FormWithValidatorV1_5 : View {
@EnvironmentObject var item:DataItem // data model reference
@StateObject var usernameValid = FieldChecker2<String>() // validation state of username field
@StateObject var passwordValid = FieldChecker2<String>() // validation state of password field
func username() -> some View {
TextField( "give me the email",
text: $item.username.onValidate(checker: usernameValid, debounceInMills: 500) { v in
// validation closure where ‘v’ is the current value
if( v.isEmpty ) {
return "email cannot be empty"
}
if( !v.isEmail() ) {
return "email is not in correct format"
}
return nil
}, onCommit: submit)
.autocapitalization(.none)
.padding( .bottom, 25 )
.modifier( ValidatorMessageModifier(message: usernameValid.errorMessage))
}
func passwordToggle() -> some View {
SecureField( "give me the password",
text:$item.password.onValidate( checker: passwordValid ) { v in
if( v.isEmpty ) {
return "password cannot be empty"
}
return nil
} )
.autocapitalization(.none)
.padding( .bottom, 25 )
.modifier( ValidatorMessageModifier(message: passwordValid.errorMessage))
}
var isValid:Bool {
passwordValid.valid && usernameValid.valid
}
func submit() {
if( isValid ) {
print( "submit:\nusername:\(self.item.username)\npassword:\(self.item.password)")
}
}
var body: some View {
NavigationView {
Form {
Section(header: Text("Credentials")) {
username()
passwordToggle()
} // end of section
Section {
HStack {
Spacer()
Button( "Submit", action: submit )
// enable button only if username and password are validb
.disabled( !self.isValid )
Spacer()
}
} // end of section
} // end of form
.navigationBarTitle( Text( "Validation 1.5 Sample" ), displayMode: .inline )
} // NavigationView
}
}
struct FormWithValidatorV1 : View {
@EnvironmentObject var item:DataItem // data model reference
@State var usernameValid = FieldChecker() // validation state of username field
@State var passwordValid = FieldChecker() // validation state of password field
@State var passwordToggleValid = FieldChecker() // validation state of password field
func username() -> some View {
TextFieldWithValidator( title: "give me the email",
value: $item.username,
checker: $usernameValid,
onCommit: submit) { v in
// validation closure where ‘v’ is the current value
if( v.isEmpty ) {
return "email cannot be empty"
}
if( !v.isEmail() ) {
return "email is not in correct format"
}
return nil
}
.autocapitalization(.none)
.padding( .bottom, 25 )
.modifier( ValidatorMessageModifier(message: usernameValid.errorMessageOrNilAtBeginning ) )
}
func passwordToggle() -> some View {
SecureFieldWithValidator( title: "give me the password"
value:$item.password,
checker:$passwordToggleValid ) { v in
if( v.isEmpty ) {
return "password cannot be empty"
}
return nil
}
.autocapitalization(.none)
.padding( .bottom, 25 )
.modifier( ValidatorMessageModifier(message: passwordToggleValid.errorMessage ) )
}
var isValid:Bool {
passwordToggleValid.valid && usernameValid.valid
}
func submit() {
if( isValid ) {
print( "submit:\nusername:\(self.item.username)\npassword:\(self.item.password)")
}
}
var body: some View {
NavigationView {
Form {
Section(header: Text("Credentials")) {
username()
passwordToggle()
} // end of section
Section {
HStack {
Spacer()
Button( "Submit" ) { self.submit() }
// enable button only if username and password are valid
.disabled( !self.isValid )
Spacer()
}
} // end of section
} // end of form
.navigationBarTitle( Text( "Sample Form" ), displayMode: .inline )
} // NavigationView
}
}