https://github.com/digipolitan/roadblock
https://github.com/digipolitan/roadblock
form swift validation
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/digipolitan/roadblock
- Owner: Digipolitan
- License: bsd-3-clause
- Created: 2017-10-24T08:12:00.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-12-13T17:25:19.000Z (almost 7 years ago)
- Last Synced: 2025-02-17T04:46:14.845Z (8 months ago)
- Topics: form, swift, validation
- Language: Swift
- Size: 58.6 KB
- Stars: 1
- Watchers: 6
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
Roadblock
=================================
[](https://developer.apple.com/swift/)
[](https://travis-ci.org/Digipolitan/roadblock)
[](https://img.shields.io/cocoapods/v/Roadblock.svg)
[](https://github.com/Carthage/Carthage)
[](http://cocoadocs.org/docsets/Roadblock)
[](http://twitter.com/Digipolitan)
Roadblock, a simple way to check forms verifications.
## Installation
### CocoaPods
To install Roadblock with CocoaPods, add the following lines to your `Podfile`.
```ruby
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!
pod 'Roadblock'
```
### Carthage
[Carthage](https://github.com/Carthage/Carthage) is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
You can install Carthage with [Homebrew](http://brew.sh/) using the following command:
```bash
$ brew update
$ brew install carthage
```
To integrate Roadblock into your Xcode project using Carthage, specify it in your `Cartfile`:
```
github 'Digipolitan/roadblock' ~> 1.0
```
Run `carthage update` to build the framework and drag the built `Roadblock.framework` into your Xcode project.
## The Basics
First to catch errors you need to add the following keys to your **Localizable.string** file
```strings
"errors.validator.required" = "Missing required field";
"errors.validator.email" = "Invalid email";
"errors.validator.min_length" = "Minimum %1$d characters";
"errors.validator.max_length" = "Maximum de %1$d characters";
```
After that, you need to implement the `FormDelegate` protocol
```swift
extension ViewController: FormDelegate {
func formDidSubmit(_ form: Form) {
self.validateButtonClicked()
}
}
```
Then you can register the form on your `viewDidLoad()` method like following
```swift
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
public var form: Form!
private var formTextFieldChainable: FormTextFieldDelegateChainable?
override func viewDidLoad() {
super.viewDidLoad()
self.form = Form(fields: [
.init(identifier: Fields.email,
target: self.emailTextField,
name: "email",
validator: FieldValidators.group(FieldValidators.required(), FieldValidators.email()),
transformer: FieldTransformers.group(FieldTransformers.trim(), FieldTransformers.emptyIsNil())),
.init(identifier: Fields.password,
target: self.passwordTextField,
name: "password",
validator: FieldValidators.group(FieldValidators.required()),
transformer: FieldTransformers.group(FieldTransformers.trim(), FieldTransformers.emptyIsNil()))
], delegate: self)
let formTextFieldChainable = FormTextFieldDelegateChainable(form: self.form)
self.emailTextField.delegate = formTextFieldChainable
self.passwordTextField.delegate = formTextFieldChainable
self.formTextFieldChainable = formTextFieldChainable
}
}
```
Then you can handle the forms errors on youre `@IBAction` function
```swift
@IBAction func validateButtonClicked() {
self.view.endEditing(true)
do {
try self.form.validate()
} catch {
if let err = error as? FormError {
let alertController = UIAlertController(title: "Error", message:
err.localizedDescription, preferredStyle: UIAlertControllerStyle.alert)
alertController.addAction(UIAlertAction(title: "Dismiss",style: UIAlertActionStyle.default, handler: nil))
self.present(alertController, animated: true, completion: nil)
}
}
return
}
```
## Contributing
See [CONTRIBUTING.md](CONTRIBUTING.md) for more details!
This project adheres to the [Contributor Covenant Code of Conduct](CODE_OF_CONDUCT.md).
By participating, you are expected to uphold this code. Please report
unacceptable behavior to [contact@digipolitan.com](mailto:contact@digipolitan.com).
## License
Roadblock is licensed under the [BSD 3-Clause license](LICENSE).