Are you a regular stikked user? Signup so you can keep track of your pastes!

CLValidation.m

By Eratic Teal, 1 Year ago, written in Objective-C.
URL http://stikked.com/view/22093762
  1. //
  2. //  CLValidation.m
  3. //  ceol
  4. //
  5. //  Created by Ben McRedmond on 24/05/2009.
  6. //  Copyright 2009 Ben McRedmond. All rights reserved.
  7. //
  8.  
  9. #import <stdarg.h>
  10. #import "CLValidation.h"
  11.  
  12. NSString * const CLValidateAlpha = @"validateAlpha:";
  13. NSString * const CLValidateAlphaSpaces = @"validateAlphaSpaces:";
  14. NSString * const CLValidateAlphaNumeric = @"validateAlphanumeric:";
  15. NSString * const CLValidateAlphaNumericDash = @"validateAlphanumericDash:";
  16. NSString * const CLValidateNotEmpty = @"validateNotEmpty:";
  17. NSString * const CLValidateEmail = @"validateEmail:";
  18.  
  19. @implementation CLValidation
  20.  
  21. - (id) init {
  22.     self = [super init];
  23.    
  24.     if(self)
  25.     {        
  26.         [self generalInit];
  27.         errorStrings = [[NSDictionary alloc] initWithObjectsAndKeys:
  28.                             @"Letters only",                        CLValidateAlpha,
  29.                             @"Letters and Spaces Only",             CLValidateAlphaSpaces,
  30.                             @"Letters and Numbers Only",            CLValidateAlphaNumeric,
  31.                             @"Letters, Numbers and Dashes Only",    CLValidateAlphaNumericDash,
  32.                             @"Can't be empty",                      CLValidateNotEmpty,
  33.                             @"Invalid Email Address",               CLValidateEmail, nil];
  34.     }
  35.    
  36.     return self;
  37. }
  38.  
  39. - (id) initWithCustomErrors: (NSDictionary *) errors {
  40.     self = [super init];
  41.    
  42.     if(self)
  43.     {
  44.         [self generalInit];
  45.         errorStrings = errors;
  46.     }
  47.    
  48.     return self;
  49. }
  50.  
  51. - (id) generalInit {
  52.     errorTable = [[NSMutableDictionary alloc] initWithCapacity:6];
  53. }
  54.  
  55. - (void) dealloc {
  56.     [errorTable release];
  57.     [errorStrings release];
  58.     [super dealloc];
  59. }
  60.  
  61. - (NSMutableArray *) validateObject: (id) object tag: (NSString *) tag rules: (NSString * const) firstRule, ... NS_REQUIRES_NIL_TERMINATION {
  62.     tempErrors = [[NSMutableArray alloc] initWithCapacity:1];
  63.  
  64.     NSString *nextRule = firstRule;
  65.    
  66.     va_list arguments;
  67.     va_start(arguments, firstRule);        
  68.    
  69.     while(nextRule)
  70.     {
  71.         [self validateRule:nextRule candidate:object tag:tag];
  72.         nextRule = va_arg(arguments, NSString * const);
  73.     }
  74.  
  75.     va_end(arguments);
  76.    
  77.     return [tempErrors autorelease];
  78. }
  79.  
  80. - (NSMutableArray *) validateObjectWithRulesAndParamaters: (id) object tag: (NSString *) tag rules: (id) firstRule, ... NS_REQUIRES_NIL_TERMINATION {
  81.     tempErrors = [[NSMutableArray alloc] initWithCapacity:1];
  82.  
  83.     id *nextObject = firstRule;
  84.    
  85.     va_list arguments;
  86.     va_start(arguments, firstRule);        
  87.    
  88.     while(nextObject)
  89.     {
  90.         [self validateRuleWithParamater:nextObject candidate:object tag:tag paramater:va_arg(arguments, id)];
  91.         nextObject = va_arg(arguments, id);
  92.     }
  93.  
  94.     va_end(arguments);
  95.    
  96.     return [tempErrors autorelease];
  97. }
  98.  
  99. - (void) validateRule: (NSString * const) rule candidate: (id) candidate tag: (NSString *) tag  {
  100.     [self validateRuleWithParamter:rule candidate: canddiate tag:tag paramater:nil];
  101. }
  102.  
  103. - (void) validateRuleWithParamter: (NSString * const) rule candidate: (id) candidate tag: (NSString *) tag paramater: (id) paramater {
  104.     SEL selector = NSSelectorFromString(rule);
  105.     BOOL isValid = [self performSelector:selector withObject:candidate];
  106.            
  107.     [self modifyErrorTable:tag method:rule isValid:isValid];
  108.     if(!isValid) [tempErrors addObject:[errorStrings objectForKey:rule]];
  109. }
  110.  
  111. - (void) modifyErrorTable: (NSString *) tag method: (NSString * const) method isValid: (BOOL) isValid {
  112.     // Check whether there's an entry already in the error table
  113.     if([errorTable objectForKey:tag] == nil)
  114.         [errorTable setObject:[NSMutableDictionary dictionaryWithCapacity:1] forKey:tag];
  115.    
  116.     // Update the 'table'
  117.     [[errorTable objectForKey:tag] setObject:[NSNumber numberWithBool:isValid] forKey:method];
  118. }
  119.  
  120. - (int) errorCount {
  121.     int errors = 0;
  122.    
  123.     NSEnumerator *enumerator = [errorTable objectEnumerator];
  124.     NSEnumerator *innerEnumerator;
  125.    
  126.     // The only objects in our table should be mutable dictionaries
  127.     NSMutableDictionary *value;
  128.     NSNumber *innerValue;    
  129.  
  130.     while((value = [enumerator nextObject]))
  131.     {
  132.         innerEnumerator = [value objectEnumerator];
  133.         while((innerValue = [innerEnumerator nextObject]))
  134.         {
  135.             if(![innerValue boolValue]) ++errors;
  136.         }
  137.     }
  138.    
  139.     return errors;
  140. }
  141.  
  142. - (int) errorCountForTag: (NSString *) tag {
  143.     int errors = 0;
  144.    
  145.     NSEnumerator *enumerator = [[errorTable objectForKey:tag] objectEnumerator];
  146.     NSNumber *value;
  147.    
  148.     while((value = [enumerator nextObject]))
  149.     {
  150.         if(![value boolValue]) ++errors;
  151.     }
  152.    
  153.     return errors;
  154. }
  155.  
  156. // ======================
  157. // = Validation Methods =
  158. // ======================
  159. - (BOOL) validateAlpha: (NSString *) toValidate {
  160.     return [self validateStringInCharacterSet:toValidate characterSet:[NSCharacterSet letterCharacterSet]];
  161. }
  162.  
  163. - (BOOL) validateAlphaSpaces: (NSString *) toValidate {
  164.     NSMutableCharacterSet *characterSet = [NSMutableCharacterSet letterCharacterSet];
  165.     [characterSet addCharactersInString:@" "];
  166.     return [self validateStringInCharacterSet:toValidate characterSet:characterSet];
  167. }
  168.  
  169. - (BOOL) validateAlphanumeric: (NSString *) toValidate {
  170.     return [self validateStringInCharacterSet:toValidate characterSet:[NSCharacterSet alphanumericCharacterSet]];
  171. }
  172.  
  173. - (BOOL) validateAlphanumericDash: (NSString *) toValidate {
  174.     NSMutableCharacterSet *characterSet = [NSMutableCharacterSet alphanumericCharacterSet];
  175.     [characterSet addCharactersInString:@"-_."];
  176.     return [self validateStringInCharacterSet:toValidate characterSet:characterSet];
  177. }
  178.  
  179. - (BOOL) validateStringInCharacterSet: (NSString *) string characterSet: (NSMutableCharacterSet *) characterSet {
  180.     // Since we invert the character set if it is == NSNotFound then it is in the character set.
  181.     return ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound) ? NO : YES;
  182. }
  183.  
  184. - (BOOL) validateNotEmpty: (NSString *) toValidate {
  185.     return ([toValidate length] == 0) ? NO : YES;
  186. }
  187.  
  188. - (BOOL) validateEmail: (NSString *) toValidate {
  189.     NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
  190.     NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
  191.  
  192.     return [emailTest evaluateWithObject:toValidate];
  193. }
  194.  
  195. @end
  196.  

Reply to "CLValidation.m"

Here you can reply to the paste above

Create a snipurl

Make Private

Feeling clever? Set some advanced options.