Validation Handling in Swift 3.0

 

User experience is the key factor for any application. Thus, More the developer is capable of delivering help, higher is the user experience. It is a mandatory for almost every application to include sign up forms,()……etc. Hence, pops up the point of validation to all these fields.

Consider a Sign up form consisting of following textfields:

  • Email
  • Name
  • Mobile no.
  • Maximum ten characters password
  • Alpha numeric field
  • Or it can be any field according to your requirement.

There can be as many approaches you could follow to validate your set of textfields. One can do all the validations on same view controller which results in a high complexity of code. But not to worry. You are at a right place to get your set of textfields validated. Continue reading to get an approach which is much more easier to implement with no more code complexity.

How ValidationManager works?

The image below showing how ValidationManager in Swift 3.0 works independently of a particular view controller.

Screen Shot 2017-02-10 at 11.34.47 AM

Steps:

  1. View Controller sends textfields inputs to the struct/class named FormvalidationModel.
  2. All the textfields input is bundles into FormValidationModel object and returns to the calling View controller.
  3. View Controller sends this FormValidationModel object to the ValidationManager for validation.
  4. If any error occurs like a field is empty or failed by the regular expression, ValidationManager sends error code and error message to the struct/class named ValidationError.
  5. The error code and error message is bundled into a ValidationError object and returns to the ValidationManager.
  6. The ValidationManager returns the ValidationError object to the View Controller.
  7. View Controller shows the validation failure if validationError contains any error message and error code in the form of alert or toast as per the requirement else the form doesn’t contain any error and is ready to be Submit which is great.

Here’s shown an example below:

Simulator Screen Shot 08-Feb-2017, 4.58.25 PM

  1. On clicking Submit button after adding values to the textfields in the set of textfields for ex- signUpForm in our case, create an instance of validation model for ex- FormValidationModel and add values of the textfields to the model in your view Controller.
  2. Pass this model as a parameter to the ValidationManager by calling validateForm(signUpModel: FormValidationModel) function and assign the value return by this function to a variable.
  3. This method returns an instance of ValidationError which contains either nil or error values such as ErrorCode and ErrorMessage.
  4. Handle this validation failure on your view controller as per the requirement.

FormValidationModel.Swift

To add this approach to your project, first of all create a swift file with name FormValidationModel following the steps below:

  • Project name folder > New File > Swift File
  • Enter name in save as field as FormValidationModel
  • Click on Create

Now open the newly created FormValidationModel class and add the code below to your file:

complete the above code by adding the following:

Here we ends up with creating a FormValidationModel.Swift. The declared parameters are used to store the values of the textfields, where these values are needed to be validated further.

Now to create FormValidationModel instance, pass all the textfield’s(which needs to be validated) text as a parameter to the constructor init() of the FormValidationModel struct.

ValidationError.Swift

Create a swift file with name ValidationError and open the newly created ValidationError file to add the code below:

This file contains all the error codes and error messages we need to show on validation failures on main view controller. You can add many more error codes and error messages according to your requirement.

The constructor of the ValidationError is called every time the string validation failure occurs which accepts two parameters naming errorCode and errorMessage and returns the ValidationError model which is to be passed to the view controller by the ValidationMananger. The view controller receives either the ValidationModel with error messages and error codes or nil. If validationError is nil that means the string validation is passed else the string validation is failed and hence we need to show the validation failure through toast or alert as per the requirement.

ValidationManager.Swift

Now create a swift file with name ValidationManager and open the newly created ValidationManager file to add the code below:

The above code declares the regex string which check if the input string satisfies the regular expression or not. You can add more regex according to your requirement.

Now add the code below to your validationManager file just after the regular expression declaration:

This method accepts a valid FormValidationModel as a parameter and is called from your view controller containing the set of textfields.

Now add the following methods below the validateForm() method to validate different fields:

You can add as many methods as you need in this Manager.

This completes the ValidationManager implemented in Swift 3.0 which can be used from any view controller where textfield validation is required.

Leave a Reply