{"id":2554,"url":"https://github.com/ustwo/formvalidator-swift","last_synced_at":"2025-04-05T14:07:15.331Z","repository":{"id":56911691,"uuid":"49581209","full_name":"ustwo/formvalidator-swift","owner":"ustwo","description":"A framework to validate inputs of text fields and text views in a convenient way.","archived":false,"fork":false,"pushed_at":"2019-10-02T06:25:58.000Z","size":1213,"stargazers_count":495,"open_issues_count":11,"forks_count":33,"subscribers_count":17,"default_branch":"master","last_synced_at":"2024-04-27T23:33:04.869Z","etag":null,"topics":["form-validation","ios","macos","tvos","ustwo-library"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ustwo.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-01-13T15:07:57.000Z","updated_at":"2024-04-13T02:52:29.000Z","dependencies_parsed_at":"2022-08-20T20:20:45.650Z","dependency_job_id":null,"html_url":"https://github.com/ustwo/formvalidator-swift","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ustwo%2Fformvalidator-swift","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ustwo%2Fformvalidator-swift/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ustwo%2Fformvalidator-swift/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ustwo%2Fformvalidator-swift/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ustwo","download_url":"https://codeload.github.com/ustwo/formvalidator-swift/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247345853,"owners_count":20924102,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["form-validation","ios","macos","tvos","ustwo-library"],"created_at":"2024-01-05T20:16:16.724Z","updated_at":"2025-04-05T14:07:15.300Z","avatar_url":"https://github.com/ustwo.png","language":"Swift","readme":"[![License](https://img.shields.io/badge/license-MIT-green.svg?style=flat)](https://github.com/ustwo/formvalidator-swift/blob/master/LICENSE.md)\n[![Build Status](https://travis-ci.org/ustwo/formvalidator-swift.svg?branch=master)](https://travis-ci.org/ustwo/formvalidator-swift)\n[![codecov.io](https://codecov.io/github/ustwo/formvalidator-swift/coverage.svg?branch=master)](https://codecov.io/github/ustwo/formvalidator-swift?branch=master)\n[![Platform](https://img.shields.io/cocoapods/p/FormValidatorSwift.svg?style=flat)](https://ustwo.github.io/formvalidator-swift)\n![Swift](https://img.shields.io/badge/Swift-5.0-orange.svg)\n[![Twitter](https://img.shields.io/badge/twitter-@ustwo-blue.svg?style=flat)](http://twitter.com/ustwo)\n\n# FormValidatorSwift\n\nThe 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](https://ustwo.com/).\n\n## Features\n\n* [x] Simply use `ValidatorTextField` instead of `UITextField` or `NSTextField` (`ValidatorTextView` instead of `UITextView` or `NSTextView`)\n* [x] Know what went wrong and where\n* [x] Create own conditions using regular expressions for example\n* [x] Create own validators which contain a collection of conditions\n* [x] Support iOS, macOS, and tvOS\n\n## Dependencies\n\n* [Xcode](https://itunes.apple.com/gb/app/xcode/id497799835?mt=12#)\n\n## Installation\n\n### CocoaPods\n\n[CocoaPods](http://cocoapods.org) is a dependency manager for Cocoa projects. You can install it with the following command:\n\n```bash\n$ gem install cocoapods\n```\n\nTo integrate FormValidatorSwift into your Xcode project using CocoaPods, specify it in your `Podfile`:\n\n```ruby\nsource 'https://github.com/CocoaPods/Specs.git'\nplatform :ios, '8.3'\nuse_frameworks!\n\npod 'FormValidatorSwift', '~\u003e 3.0'\n```\n\nThen, run the following command:\n\n```bash\n$ pod install\n```\n\n### Manually\n\nIf you prefer not to use either of the aforementioned dependency managers, you can integrate FormValidatorSwift into your project manually.\n\n#### Embedded Framework\n\n* 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:\n\n```bash\n$ git init\n```\n\n* Add FormValidatorSwift as a git [submodule](http://git-scm.com/docs/git-submodule) by running the following command:\n\n```bash\n$ git submodule add https://github.com/ustwo/formvalidator-swift.git\n```\n\n* Open the new `FormValidatorSwift` folder, and drag the `FormValidatorSwift.xcodeproj` into the Project Navigator of your application's Xcode project.\n\n  \u003e 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.\n\n* Select the `FormValidatorSwift.xcodeproj` in the Project Navigator and verify the deployment target matches that of your application target.\n* 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.\n* In the tab bar at the top of that window, open the \"General\" panel.\n* Click on the `+` button under the \"Embedded Binaries\" section.\n* You will see two different `FormValidatorSwift.xcodeproj` folders each with two different versions of the `FormValidatorSwift.framework` nested inside a `Products` folder.\n\n  \u003e It does not matter which `Products` folder you choose from, but it does matter whether you choose the top or bottom `FormValidatorSwift.framework`.\n\n* Select the top `FormValidatorSwift.framework` for iOS, the middle one for tvOS, or the bottom one for macOS.\n\n  \u003e 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`.\n\n* And that's it!\n\n\u003e 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.\n\n---\n\n## Usage\n\nThe two core components of FormValidatorSwift are `Condition` and `Validator`. These are both protocols, with many common implementations provided by the framework.\n\nA `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.\n\n`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.\n\n### Condition\n\nA `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.\n\nHere 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.\n\n```swift\nlet condition = AlphanumericCondition()\n\nlet validResult = condition.check(\"Foo123\")\nlet invalidResult = condition.check(\"Foo?!@\")\n```\n\n### Validator\n\nA `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.\n\nHere is an example using one of the built-in validators. In this example, `validResult` will be `nil` and `invalidResult` will be `[AlphanumericCondition]`.\n\n```swift\nlet validator = AlphanumericValidator()\n\nlet validResult = validator.checkConditions(\"Foo123\")\nlet invalidResult = validator.checkConditions(\"Foo?!@\")\n```\n\n### ValidatorTextField\n\nTo 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.\n\nHere is an example of a text field that would only allow alphanumeric text.\n\n```swift\nlet nameTextField = ValidatorTextField(validator: AlphanumericValidator())\n```\n\nThis 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:\n\n```swift\nlet emailTextField = ValidatorTextField(validator: EmailValidator())\nemailTextField.shouldAllowViolation = true\nemailTextField.validateOnFocusLossOnly = true\n```\n\nWe 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.\n\n```swift\nfunc validatorControl(validatorControl: ValidatorControl, changedValidState validState: Bool) {\n    guard let controlView = validatorControl as? UIView else {\n        return\n    }\n\n    if validState {\n        controlView.layer.borderColor = nil\n        controlView.layer.borderWidth = 0.0\n        errorLabel.hidden = true\n    } else {\n        controlView.layer.borderColor = UIColor.red.CGColor\n        controlView.layer.borderWidth = 2.0\n    }\n}\n\nfunc validatorControl(validatorControl: ValidatorControl, violatedConditions conditions: [Condition]) {\n    var errorText = \"\"\n    for condition in conditions {\n        errorText += condition.localizedViolationString\n    }\n    errorLabel.text = errorText\n\n    errorLabel.hidden = false\n}\n\nfunc validatorControlDidChange(validatorControl: ValidatorControl) {\n    // Not used in this example\n}\n```\n\n### Form\n\nWe 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:\n\n```swift\nvar form = ControlForm()\n\nform.addEntry(nameTextField)\nform.addEntry(emailTextField)\n\nif form.isValid {\n  // Hooray! Our form is valid. Submit the data!\n  ...\n} else {\n  // Sad day, we need to have the user fix the data.\n  ...\n}\n```\n\n### Example\n\nMore detailed examples can be found in the `iOS Example` and `macOS Example` apps in this repository.\n\n## Collaborate\n\nWe 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][contributing] to find out how to add your support.\n\n## Maintainers\n\n* Shagun Madhikarmi (@madhikarma)\n* Aaron McTavish (@aamctustwo)\n\n## Contact\n\n* [open.source@ustwo.com](mailto:open.source@ustwo.com)\n\n## License\n\nFormValidatorSwift is released under the MIT License. See the LICENSE file.\n\n\u003c!-- Links --\u003e\n\n[contributing]: .github/CONTRIBUTING.md\n","funding_links":[],"categories":["UI","Libs","Validation [🔝](#readme)"],"sub_categories":["Form \u0026 Settings","Validation","Other free courses"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fustwo%2Fformvalidator-swift","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fustwo%2Fformvalidator-swift","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fustwo%2Fformvalidator-swift/lists"}