All Projects β†’ ustwo β†’ Formvalidator Swift

ustwo / Formvalidator Swift

Licence: mit
A framework to validate inputs of text fields and text views in a convenient way.

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Formvalidator Swift

Sentry Cocoa
The official Sentry SDK for iOS, tvOS, macOS, watchOS
Stars: ✭ 370 (-25.25%)
Mutual labels:  tvos
Userdefaultsstore
Why not use UserDefaults to store Codable objects πŸ˜‰
Stars: ✭ 416 (-15.96%)
Mutual labels:  tvos
Gridstack
A flexible grid layout view for SwiftUI
Stars: ✭ 474 (-4.24%)
Mutual labels:  tvos
Acknowlist
Acknowledgements screen displaying a list of licenses, for example from CocoaPods dependencies.
Stars: ✭ 392 (-20.81%)
Mutual labels:  tvos
Fiber2d
Cross-platform 2D Game Engine in pure Swift
Stars: ✭ 415 (-16.16%)
Mutual labels:  tvos
Swiftuipager
Native Pager in SwiftUI
Stars: ✭ 430 (-13.13%)
Mutual labels:  tvos
Valet
Valet lets you securely store data in the iOS, tvOS, or macOS Keychain without knowing a thing about how the Keychain works. It’s easy. We promise.
Stars: ✭ 3,712 (+649.9%)
Mutual labels:  tvos
Provenance
iOS & tvOS multi-emulator frontend, supporting various Atari, Bandai, NEC, Nintendo, Sega, SNK and Sony console systems… Get Started: https://wiki.provenance-emu.com |
Stars: ✭ 4,732 (+855.96%)
Mutual labels:  tvos
Waterwheel.swift
The Waterwheel Swift SDK provides classes to natively connect iOS, macOS, tvOS, and watchOS applications to Drupal 7 and 8.
Stars: ✭ 415 (-16.16%)
Mutual labels:  tvos
Telegraph
Secure Web Server for iOS, tvOS and macOS
Stars: ✭ 474 (-4.24%)
Mutual labels:  tvos
Impact
Crash capturing library for Apple platforms
Stars: ✭ 395 (-20.2%)
Mutual labels:  tvos
Efqrcode
A better way to operate QR Code in Swift, support iOS, macOS, watchOS and tvOS.
Stars: ✭ 4,121 (+732.53%)
Mutual labels:  tvos
Stringz
A lightweight and powerful editor for localizing iOS, macOS, tvOS, and watchOS applications.
Stars: ✭ 440 (-11.11%)
Mutual labels:  tvos
React Hook Form
πŸ“‹ React Hooks for form state management and validation (Web + React Native)
Stars: ✭ 24,831 (+4916.36%)
Mutual labels:  form-validation
Googlereporter
Easily integrate with Google Analytics in your iOS app
Stars: ✭ 479 (-3.23%)
Mutual labels:  tvos
Xcglogger
A debug log framework for use in Swift projects. Allows you to log details to the console (and optionally a file), just like you would have with NSLog() or print(), but with additional information, such as the date, function name, filename and line number.
Stars: ✭ 3,710 (+649.49%)
Mutual labels:  tvos
Mtlpp
C++ Metal wrapper
Stars: ✭ 425 (-14.14%)
Mutual labels:  tvos
Swiftyutils
All the reusable code that we need in each project
Stars: ✭ 490 (-1.01%)
Mutual labels:  tvos
Corexlsx
Excel spreadsheet (XLSX) format parser written in pure Swift
Stars: ✭ 481 (-2.83%)
Mutual labels:  tvos
Bunny
BunnyJS - Lightweight native (vanilla) JavaScript (JS) and ECMAScript 6 (ES6) browser library, package of small stand-alone components without dependencies: FormData, upload, image preview, HTML5 validation, Autocomplete, Dropdown, Calendar, Datepicker, Ajax, Datatable, Pagination, URL, Template engine, Element positioning, smooth scrolling, routing, inversion of control and more. Simple syntax and architecture. Next generation jQuery and front-end framework. Documentation and examples available.
Stars: ✭ 473 (-4.44%)
Mutual labels:  form-validation

License Build Status codecov.io Platform Swift Twitter

FormValidatorSwift

The FormValidatorSwift framework allows you to validate inputs of text fields and text views in a convenient way. It has been developed and used by iOS developers at ustwo.

Features

  • [x] Simply use ValidatorTextField instead of UITextField or NSTextField (ValidatorTextView instead of UITextView or NSTextView)
  • [x] Know what went wrong and where
  • [x] Create own conditions using regular expressions for example
  • [x] Create own validators which contain a collection of conditions
  • [x] Support iOS, macOS, and tvOS

Dependencies

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate FormValidatorSwift into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.3'
use_frameworks!

pod 'FormValidatorSwift', '~> 3.0'

Then, run the following command:

$ pod install

Manually

If you prefer not to use either of the aforementioned dependency managers, you can integrate FormValidatorSwift into your project manually.

Embedded Framework

  • Open up Terminal, cd into your top-level project directory, and run the following command "if" your project is not initialized as a git repository:
$ git init
  • Add FormValidatorSwift as a git submodule by running the following command:
$ git submodule add https://github.com/ustwo/formvalidator-swift.git
  • Open the new FormValidatorSwift folder, and drag the FormValidatorSwift.xcodeproj into the Project Navigator of your application's Xcode project.

    It should appear nested underneath your application's blue project icon. Whether it is above or below all the other Xcode groups does not matter.

  • Select the FormValidatorSwift.xcodeproj in the Project Navigator and verify the deployment target matches that of your application target.

  • Next, select your application project in the Project Navigator (blue project icon) to navigate to the target configuration window and select the application target under the "Targets" heading in the sidebar.

  • In the tab bar at the top of that window, open the "General" panel.

  • Click on the + button under the "Embedded Binaries" section.

  • You will see two different FormValidatorSwift.xcodeproj folders each with two different versions of the FormValidatorSwift.framework nested inside a Products folder.

    It does not matter which Products folder you choose from, but it does matter whether you choose the top or bottom FormValidatorSwift.framework.

  • Select the top FormValidatorSwift.framework for iOS, the middle one for tvOS, or the bottom one for macOS.

    You can verify which one you selected by inspecting the build log for your project. The build target for FormValidatorSwift will be listed as FormValidatorSwift iOS, FormValidatorSwift macOS, or FormValidatorSwift tvOS.

  • And that's it!

The FormValidatorSwift.framework is automagically added as a target dependency, linked framework and embedded framework in a copy files build phase which is all you need to build on the simulator and a device.


Usage

The two core components of FormValidatorSwift are Condition and Validator. These are both protocols, with many common implementations provided by the framework.

A Condition defines a specific requirement for a String to be considered valid and defines a way to check the String. A Validator defines a way to check whether a String is valid based on a set of Condition. These provide the building blocks upon which the other elements of FormValidatorSwift are built.

ValidatorTextField and ValidatorTextView provide common UI implementations for a validatable text input method. These controls can then be combined into a Form for quick validation of all text input.

Condition

A Condition is typically defined by a regular expression. This is used in the default implementation to check the string. However, you can provide your own implementation of the check(text:) function to do a more complicated validation.

Here is an example using one of the built-in conditions. Note that calling check(text:) simply returns a Bool as to whether the text is valid or not.

let condition = AlphanumericCondition()

let validResult = condition.check("Foo123")
let invalidResult = condition.check("[email protected]")

Validator

A Validator takes an array of Condition and checks each condition to validate a string. If the validation fails, then checkConditions(text:) will return an array of the violated conditions.

Here is an example using one of the built-in validators. In this example, validResult will be nil and invalidResult will be [AlphanumericCondition].

let validator = AlphanumericValidator()

let validResult = validator.checkConditions("Foo123")
let invalidResult = validator.checkConditions("[email protected]")

ValidatorTextField

To provide a user interface, you can use ValidatorTextField or ValidatorTextView. These are subclasses of UITextField and UITextView respectively (or NSTextField and NSTextView on macOS). They both conform to the ValidatorControl protocol, which has the additional capability of using a Validator to check the text.

Here is an example of a text field that would only allow alphanumeric text.

let nameTextField = ValidatorTextField(validator: AlphanumericValidator())

This does not work well for more complicated text fields. For example, you would not want an email address validated until the user is finished typing. To postpone validation, we need to set shouldAllowViolation and validateOnFocusLossOnly both to be true. Example:

let emailTextField = ValidatorTextField(validator: EmailValidator())
emailTextField.shouldAllowViolation = true
emailTextField.validateOnFocusLossOnly = true

We can respond to changes in the validity of ValidatorTextField by implementing the ValidatorControlDelegate and setting ourselves as the validator delegate (using the setValidatorDelegate(_:) method). Below is an example implementation. In the example we highlight the text field with a red border if it is invalid. We also list the error in a label called errorLabel and present it to the user.

func validatorControl(validatorControl: ValidatorControl, changedValidState validState: Bool) {
    guard let controlView = validatorControl as? UIView else {
        return
    }

    if validState {
        controlView.layer.borderColor = nil
        controlView.layer.borderWidth = 0.0
        errorLabel.hidden = true
    } else {
        controlView.layer.borderColor = UIColor.red.CGColor
        controlView.layer.borderWidth = 2.0
    }
}

func validatorControl(validatorControl: ValidatorControl, violatedConditions conditions: [Condition]) {
    var errorText = ""
    for condition in conditions {
        errorText += condition.localizedViolationString
    }
    errorLabel.text = errorText

    errorLabel.hidden = false
}

func validatorControlDidChange(validatorControl: ValidatorControl) {
    // Not used in this example
}

Form

We can combine a series of ValidatorControl into a Form. We have a convenience implementation call ControlForm. We can then combine our alphanumeric textfield and our email textfield from our previous examples into a form. This provides an easy method for checking if the entire form is valid (say, before submission of the form data to a server). Below is an example:

var form = ControlForm()

form.addEntry(nameTextField)
form.addEntry(emailTextField)

if form.isValid {
  // Hooray! Our form is valid. Submit the data!
  ...
} else {
  // Sad day, we need to have the user fix the data.
  ...
}

Example

More detailed examples can be found in the iOS Example and macOS Example apps in this repository.

Collaborate

We welcome contributors! Whether you're fixing a typo, squashing a bug, or adding new functionality please join us in making this project better. Read our contributing guidelines to find out how to add your support.

Maintainers

Contact

License

FormValidatorSwift is released under the MIT License. See the LICENSE file.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].