A better approach to UITextField validations on iOS

Arlind Aliu
Swift2Go
Published in
3 min readAug 5, 2018

Validating Text Fields is a case that comes across many times while developing mobile apps, whether its something simple as login in or registering on the application, to posting data to the server from multiple field forms.
This article is written to provide you with information on how field validation on iOS can become really simple, and avoid code duplication and separate validation logic from view code.

Problem

A classic approach of validating and submitting fields can create unreadable code and code duplication across the app. With code refactoring that this article will present you can achieve one-line text field validations and reusable validation code across the app.

A simple example of validating text field data can be something like following:

This piece of code looks pretty messy already to be part of the view controller, and imagine cases when there are multiple fields to be validated. More and more code will be added to our view controller.

Firstly we can try to refactor this function to a separate Validator class so validation code gets removed from view code and also it provides some reusability

And then try to call it

Separating validator logic to its separate class provided a little cleanup but still, there is a lot of code for a field validation and also “Invalid Email” alert needs to be rewritten on every place isValidEmail() gets used. We want more general error messages that can be reused throughout the app.

We can try to throw validation errors to generalize errors messages of specific invalid fields.

Our function then can throw a validation error in case of an invalid field.

Validator class looks good for now but when lots of new validators get introduced, the class will start to look messy.

We can move validator logic to its own struct and introduce an abstract factory that is responsible for creating validators depending on the field type.

Next, on our controller we can validate our fields like following:

This already looks more clean and readable but there are two problems first code is still not minimal and second, we have to create on our view code validators from factories for each field, to get validated data.

Using Extensions to cleanup the code

One of the huge benefits of swift language are Extensions. Most important thing is that Extensions allow you to extend already available api without the need to subclass specific objects and have to use custom objects everywhere just to add a new method.

We can use the power of extensions to provide more readable code for us:

and our final code can look something like this:

Suppose that we want to add validation for multiple fields of a form, our code would look something like the example below:

In Conclusion

We now have created reusable validators and separated logic of validation to its own layer. Adding new validators becomes really easy by just creating a struct that conforms to theValidatorConvertible and we provide its creation from our validator factory.

I hope that you enjoyed this post and this type of validation will become useful to your code base.

If you want to access the complete project and demo code that provides many more validators, you can find it on Github
https://github.com/Arrlindii/AAValidators

If you have any questions or comments feel free to leave a note here or email me at arlindaliu.dev@gmail.com.

--

--

Arlind Aliu
Swift2Go

I spend half of my life coding, the other half I waste.