Last active
December 20, 2015 16:49
-
-
Save xNekOIx/6163936 to your computer and use it in GitHub Desktop.
Form Validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
extern NSString* const PSFormValidationErrorDomain; | |
typedef NS_ENUM(NSInteger, PSFormValidationError) { | |
PSFormValidationErrorUndefined = 0, | |
PSFormValidationErrorEmailEmpty, | |
PSFormValidationErrorUsernameEmpty, | |
PSFormValidationErrorPasswordEmpty, | |
PSFormValidationErrorPasswordTooShort, | |
PSFormValidationErrorPasswordTooLong, | |
PSFormValidationErrorConfirmPasswordEmpty, | |
PSFormValidationErrorConfirmPasswordDoesNotMatch | |
}; | |
@interface PSFormValidator : NSObject | |
- (BOOL)validateEmail:(id*)value error:(NSError* __autoreleasing*)error; | |
- (BOOL)validateUsername:(id*)value error:(NSError* __autoreleasing*)error; | |
- (BOOL)validatePassword:(id*)value error:(NSError* __autoreleasing*)error; | |
- (BOOL)validatePasswordConfirmation:(id*)confirmPassword password:(id*)password error:(NSError* __autoreleasing*)error; // is not KVC compatible | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "PSFormValidator.h" | |
NSString* const PSFormValidationErrorDomain = @"PSValidationErrorDomain"; | |
@implementation PSFormValidator | |
- (BOOL)validateEmail:(id*)value error:(NSError* __autoreleasing*)error | |
{ | |
if ((*value == nil) || ([*value length] == 0)) | |
{ | |
if (error != NULL) | |
{ | |
NSString* errorString = L(@"%sign_up_error_mail"); | |
NSDictionary* userInfoDict = @{ NSLocalizedDescriptionKey: errorString }; | |
*error = [[NSError alloc] initWithDomain:PSFormValidationErrorDomain | |
code:PSFormValidationErrorEmailEmpty | |
userInfo:userInfoDict]; | |
} | |
return NO; | |
} | |
return YES; | |
} | |
- (BOOL)validateUsername:(id*)value error:(NSError* __autoreleasing*)error | |
{ | |
if ((*value == nil) || ([*value length] == 0)) | |
{ | |
if (error != NULL) | |
{ | |
NSString* errorString = L(@"%sign_up_error_username"); | |
NSDictionary* userInfoDict = @{ NSLocalizedDescriptionKey: errorString }; | |
*error = [[NSError alloc] initWithDomain:PSFormValidationErrorDomain | |
code:PSFormValidationErrorUsernameEmpty | |
userInfo:userInfoDict]; | |
} | |
return NO; | |
} | |
return YES; | |
} | |
- (BOOL)validatePassword:(id*)value error:(NSError* __autoreleasing*)error | |
{ | |
if ((*value == nil) || ([*value length] == 0)) | |
{ | |
if (error != NULL) | |
{ | |
*error = [[NSError alloc] initWithDomain:PSFormValidationErrorDomain | |
code:PSFormValidationErrorPasswordEmpty | |
userInfo:@{ NSLocalizedDescriptionKey: L(@"%sign_up_error_password_empty") }]; | |
} | |
return NO; | |
} | |
if ([*value length] < 6) | |
{ | |
if (error != NULL) | |
{ | |
*error = [[NSError alloc] initWithDomain:PSFormValidationErrorDomain | |
code:PSFormValidationErrorPasswordTooShort | |
userInfo:@{ NSLocalizedDescriptionKey: L(@"%sign_up_error_password_short") }]; | |
} | |
return NO; | |
} | |
if ([*value length] > 20) | |
{ | |
if (error != NULL) | |
{ | |
*error = [[NSError alloc] initWithDomain:PSFormValidationErrorDomain | |
code:PSFormValidationErrorPasswordTooLong | |
userInfo:@{ NSLocalizedDescriptionKey: L(@"%sign_up_error_password_long") }]; | |
} | |
return NO; | |
} | |
return YES; | |
} | |
- (BOOL)validatePasswordConfirmation:(id*)confirmPassword password:(id*)password error:(NSError* __autoreleasing*)error; | |
{ | |
if ((*confirmPassword == nil) || ([*confirmPassword length] == 0)) | |
{ | |
if (error != NULL) | |
{ | |
*error = [[NSError alloc] initWithDomain:PSFormValidationErrorDomain | |
code:PSFormValidationErrorConfirmPasswordEmpty | |
userInfo:@{ NSLocalizedDescriptionKey: L(@"%sign_up_error_confirm_password_empty") }]; | |
} | |
return NO; | |
} | |
if (![*confirmPassword isEqualToString:*password]) | |
{ | |
if (error != NULL) | |
{ | |
*error = [[NSError alloc] initWithDomain:PSFormValidationErrorDomain | |
code:PSFormValidationErrorConfirmPasswordDoesNotMatch | |
userInfo:@{ NSLocalizedDescriptionKey: L(@"%sign_up_error_password_not_match") }]; | |
} | |
return NO; | |
} | |
return YES; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment