Swiftpack.co - futuredapp/FormStateKit as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
futuredapp/FormStateKit
A Swift package for simple management of forms and their fields. Focused on SwiftUI.
.package(url: "https://github.com/futuredapp/FormStateKit.git", from: "0.2.0")

FormStateKit

A Swift package for simple management of forms and its fields. Focused on SwiftUI.

Features

  • Declarative form and validation definition.
  • Single state for the whole form with its fields.
  • Support for non-String validations and fields like toggles, sliders, selection, multi-selection and more.
  • Cross-field validation, comparing values of more fields with each other (like password and password again fields).
  • Some standard built-in validations (required, empty, email and methods for combining validation rules).
  • Works nicely with the new @FocusState APIs.

Usage

You can define a form state like this as a simple struct instead of defining more state properies for each field:

struct SignupForm {
    var name: String
    var email: String
    var password: String
    var acceptedPrivacyPolicy: Bool
}

To setup prefilled values for the field you can use default property values (or some static instance if you prefer):

struct SignupForm {
    var name = String()
    var email = String()
    var password = String()
    var acceptedPrivacyPolicy = false
}

To declare validation logic for the form you can create an array of validations:

let validations: [FormValidation<SignupForm>] = [
    FormValidation(for: \.name, description: "Name must be filled in.", rule: .required),
    FormValidation(for: \.email, description: "Email must be filled in.", rule: .required),
    FormValidation(for: \.email, description: "Email is not in in valid format.", rule: .email),
    FormValidation(for: \.password, description: "Password is required.", rule: .required),
    FormValidation(for: \.password, description: "Password must be longer than 6 characters") { $0.count > 6 },
    FormValidation(for: \.acceptedPrivacyPolicy, description: "You need to accept privacy policy.", rule: .hasToBeOn)
]

And if you want to use the package for focus management you can define array of key paths as a field order.

let fieldOrder: [PartialKeyPath<Self>] = [\.name, \.email, \.password]

When you have your form declared you can you it in the view:

struct SignupView: View {
    @State private var state = FormState(form: SignupForm())

    @FocusState private var focus: PartialKeyPath<SignupForm>?

    var body: some View {
        Form {
            Section {
                TextField("Name", text: $state.form.name)
                    .focused($focus, equals: \.name)
                    .onSubmit {
                        state.submit(field: \.name, updating: &focus)
                    }
                ForEach(errors, id: \.self, content: Text.init)
                    .font(.footnote)
                    .foregroundColor(.red)
            }

            ...

            Section {
                Button(action: submit) {
                    Label("Submit", systemImage: "arrow.right")
                }
            }
        }
    }

    private func submit() {
        if state.validate() {
            ...
        } else {
            ...
        }
    }
}

Installation

When using Swift package manager install using Xcode 11+ or add following line to your dependencies:

.package(url: "https://github.com/futuredapp/FormStateKit.git", from: "0.1.0")

Contributing

All contributions are welcome.

Current maintainer and main contributor is Matěj Kašpar Jirásek, matej.jirasek@futured.app.

License

FormStateKit is available under the MIT license. See the LICENSE file for more information.

GitHub

link
Stars: 2
Last commit: 1 week 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

Improved platform support
1 week ago

Fix iOS release compilation due to strange build system behavior.

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