Swiftpack.co -  bsorrentino/swiftui-fieldvalidator as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
bsorrentino/swiftui-fieldvalidator
SwiftUI Package supporting "Form Validation"
.package(url: "https://github.com/bsorrentino/swiftui-fieldvalidator.git", from: "v1.5.0")

Field Validator Library for SwiftUI

Badge w/ Version Badge w/ Platform

SwiftUI Package supporting "Form Validation"

Take a look to Build field validator for SwiftUI and in folder Example for a reference implementation

Import library

Swift Package Manager

This Library is compatible with Swift Package Manager

Cocoapods

This Library is compatible with Cocoapods.

In your Podfile add

pod 'FieldValidatorLibrary', '~> 1.5.0'

Sample

Version 1.5.x

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
    }
}

Version 1.4.x


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
    }
}

Sample

GitHub

link
Stars: 39
Last commit: 2 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Release Notes

1.5.0
2 weeks ago

add a new implementation based upon Binding extensions

add debounce time before start validation #3

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics