Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS VIPER: where to put the form validation code?

I read many articles about the clean iOS architecture VIPER, I understand the main puropose: the separation of concerns.

I currently use it for my project. I have modules, each split by Views, Interactors, Presenters, Entities and Routers (with Storyboard).

I have a module: Address and a submodule Add for the add address page.

So, I have my protocol View implemented by my UIViewController. The view controller holds all weak IBOutlet labels and text fields (for the new address form).

The address form contains few fields like:

  • person first name and last name
  • postcode
  • country
  • state
  • phone
  • email
  • etc...

In my case the presenter just relies user interactions to the interactor which performs the API call.

But, before performing the API call, I want to pre-validate the form to avoid consume useless network resource.

I need to check for example:

  • the content of country and tell to the view that field is required if empty...
  • the format of email and tell to the view that field is invalid...

My question is, where can I put my form validation code?

Which VIPER component should be fill that job?

Thank you in advance!

like image 342
ludriv Avatar asked May 23 '16 21:05

ludriv


People also ask

How do you add validation to a form?

The simplest HTML validation feature is the required attribute. To make an input mandatory, add this attribute to the element. When this attribute is set, the element matches the :required UI pseudo-class and the form won't submit, displaying an error message on submission when the input is empty.

What is form validation?

Form validation is a “technical process where a web-form checks if the information provided by a user is correct.” The form will either alert the user that they messed up and need to fix something to proceed, or the form will be validated and the user will be able to continue with their registration process.

What is Viper architecture iOS?

VIPER is an application of Clean Architecture to iOS apps. The word VIPER is a backronym for View, Interactor, Presenter, Entity, and Routing. Clean Architecture divides an app's logical structure into distinct layers of responsibility.


2 Answers

One of the key benefits from VIPER is separation of concerns, since information is encapsulated in the appropriate element.

The Interactor deals with the "business logic", which includes most of the validation concern (part of that validation can possibly done by the Entity itself). The view would therefore pass its data to the Presenter, which would refer it to the Interactor, which would check its Business Validity and ask the Entity to inform it about the coherence of the data.

However, use of libraries to speed your developments can force you to trade off encapsulation for ease of use. For example, SwiftValidator offers pretty extensive validation rules, but require you to pass your UITextFields to the Validator component.

The choice is therefore yours, between a better encapsulated architecture relying on an Interactor (possibly beefed by a library like Validators), or a more MVVM-oriented tool like SwiftValidator.

like image 124
Kheldar Avatar answered Sep 18 '22 10:09

Kheldar


Since this is related to business logic, I would say that validation should go to interactor. You can even create worker that will be called form interactor - if your validation is too big, but interactor is the place to do it. With delegates you can notify user if something is wrong, what exactly is wrong, error message etc.

I would refer to http://clean-swift.com/clean-swift-ios-architecture/ as well.

like image 38
Miknash Avatar answered Sep 20 '22 10:09

Miknash