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:
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:
My question is, where can I put my form validation code?
Which VIPER component should be fill that job?
Thank you in advance!
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.
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.
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.
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 UITextField
s 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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With