Swiftpack.co - bsorrentino/swiftui-fieldvalidator as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by bsorrentino.
bsorrentino/swiftui-fieldvalidator v1.5.0
SwiftUI Package supporting "Form Validation"
ā­ļø 53
šŸ•“ 2 years ago
iOS macOS watchOS tvOS
.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: 53
Last commit: 2 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

1.5.0
2 years 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