I have recently commenced working on a Swift (2) iOS project, and started to face circumstances where I have forms with many fields which need validating.
I have a predominantly .Net background, and with Binding and Annotations it is possible to implement form validation cleanly in a low maintenance manner, on large forms, with few lines of code.
Since embracing Swift, I have come across many examples detailing validation in various fashions, but everything I have encountered thus far seems very labour intensive and high maintenance, or the explanations focus on conducting the validation itself rather than connecting the validation process between the model and the view efficiently.
For example:
My current solution defines extensions which can be checked in a function, rule-by-rule, when validating field input, however I feel there must be a more scalable solution to this problem.
What approaches can be taken, when validating forms with potentially many inputs, that promote maintainability?
For example sake, we could discuss a hypothetical form with:
- A textField requesting a number be entered (Int).
- A textField requesting a decimal number be entered (Double).
- A textField requesting a number be entered, matching specific rules (Eg, is prime) (Double).
- A textField requesting a string be entered of some length kL.
- A textField requesting a string be entered conforming to some custom rule.
Of course, I am not looking for an implementation that literally satisfies the list above, but instead a method or approach that is scaleable across these types of scenarios.