Easy Form Validation in Swift

I come from a very web-centric background and have used frameworks like Laravel in the past extensively. One of the things I really loved about that framework in particular was the way in which it handled validation.

If you don't know, Laravel just takes an associative array (think: dictionary) of field names and a set of rules. I looked for a similar class or package to do the same in Swift but couldn't find anything so I wrote something to do it.

SRFormValidator

If you can't be fucked to read the rest of the post and want to get straight to the goods then I've put it up on GitHub.

SRFormValidator is a pretty barebones class but it has 5 rules that can easily be validated. Just like Laravel, it takes a dictionary of fields and rules. For the key, it takes a keyPath and for the value it takes your rules.

Here's an example that looks at two dictionaries and has multiple rules.

let rules = [
    "personalDetails.emailAddress": "required|email",
    "personalDetails.firstName": "required",
    "personalDetails.lastName": "required",
    "personalDetails.phone": "required|min:10|max:30",
    "shippingDetails.address": "required",
    "shippingDetails.city": "required",
    "shippingDetails.state": "required",
    "shippingDetails.zipCode": "required|alphanumeric|max:10"
]

If you're at all familiar with the Laravel validator, you'll be right at home here. Multiple rules are separated with a pipe symbol and in the case of min and max rules you can pass an argument with a colon.

let errors = SRFormValidator.isValid(rules, self)
if(errors != nil) {
    println(errors)
}

There's one public class method isValid that takes a set of rules and a target class. It'll return nil or an array of keyPaths to the fields that didn't validate. In my project, I've been passing these errors on to my UITableViewController through a delegate and then displaying a message.

Rules

here are currently 5 rules that can be used to validate your fields. Multiple rules can be used in tandem when separated with a |. Some rules use a : to include arguments.

Rule Effect
required Checks if the field is nil or a blank string.
email Checks if the field is a valid email address using regex.
alphanumeric Checks if the field only contains a-z, A-Z, and 0-9 using regex.
min:10 Checks if the the field has a character length minimum to the value set or if an integer whether the value is >= to the rule.
max:10 Checks if the the field has a character length maximum to the value set or if an integer whether the value is <= to the rule.

Installation

Either fetch the class and integrate it manually. Or — more sensibly — use Cocoapods.

pod 'SRFormValidator'

Notes

  • Built for Swift 1.2 and tested in Xcode 6.3. Swift 2.0 version will take over the master branch after iOS 9 is finalised.
  • Can only currently validate strings. It'll probably break if you try anything else but you can give it a go. Min and Max will try to parse ints.
  • Messages aren't currently returned, only that the field has failed.

✌️

Come Say Hello! Drop me an email, follow me on Twitter, or check out Cocoon (you totally should, we're doing some cool stuff over there).


About Stephen Radford

Software Engineer. I take a few photos when I'm not stuck behind a keyboard too.

Leicester, UK http://stephenradford.co.uk

Comments