I wonder if already exist a generic library of validator suitable for a iPhone project (or better, bussines objects) for obj-c.
Things like:
Validator.IsNotEmpty.. Validator.IsValidEmail.. Validator.IsValidZipCode.. Validator.IsValidRegex.. Validator.IsInRange..
There is a standard validation pattern in Cocoa, including Cocoa Touch, called Key-Value Validation that you should conform to if you want to validate user input other than through an NSFormatter.
In essence, for a property
namethat you want validated, you implement a method following this pattern:Then, when you want to validate a property before setting it from your UI, you just use:
Note that you do not invoke
-validateValue:forKey:error:or-validateName:error:method in your-setName:method. Instead, you invoke your-setName:method (as I do via dot syntax above) once you know the value you’re passing for it will be valid.Also, read the document on Key-Value Validation that I mentioned above to understand why
proposedNameis an autoreleased copy of a field’s string value, instead of just its string value.If you’re implementing something more complicated than per-property validation, such as whole-object validation or even object graph validation, be sure to see how the Core Data framework on Mac OS X handles it.