|
|
1.1 ! root 1: #import "ValidatingDelegate.h" ! 2: #import "KeyValueValidation.h" ! 3: ! 4: @implementation ValidatingDelegate ! 5: ! 6: - (void)setValidatesImmediately:(BOOL)yn ! 7: { ! 8: validatesImmediately = yn; ! 9: } ! 10: ! 11: - (BOOL)validatesImmediately ! 12: { ! 13: return validatesImmediately; ! 14: } ! 15: ! 16: - (void)reportValidationError:(NSString *)message forKey:(NSString *)key ofObject:object inController:(EOController *)controller ! 17: { ! 18: NSString *title; ! 19: title = [NSString stringWithFormat:@"Invalid input for: %@", key]; ! 20: ! 21: // Let's make sure that the object with the error is selected ! 22: [controller setSelectionIndexes:[NSArray arrayWithObject: ! 23: [NSNumber numberWithInt:[[controller allObjects] indexOfObjectIdenticalTo:object]]]]; ! 24: ! 25: NXRunAlertPanel([title cString], [message cString], "OK", NULL, NULL); ! 26: ! 27: // we could tell the controller to select the invalid object and ! 28: // run through the associations and restore focus to the one ! 29: // matching the key ! 30: } ! 31: ! 32: - (void)reportValidationErrors:(NSDictionary *)errors forObject:object inController:(EOController *)controller ! 33: { ! 34: // pick one of the errors and display it ! 35: NSEnumerator *errorEnum = [errors keyEnumerator]; ! 36: NSString *badValueKey, *errorMessage; ! 37: badValueKey = [errorEnum nextObject]; ! 38: errorMessage = [errors objectForKey:badValueKey]; ! 39: ! 40: [self reportValidationError:errorMessage forKey:badValueKey ofObject:object ! 41: inController:controller]; ! 42: } ! 43: ! 44: - (NSDictionary *)controller:(EOController *)controller willSaveEdits: (NSDictionary *)edits toObject:object; ! 45: { ! 46: if (validatesImmediately) { ! 47: NSMutableDictionary *converted = [NSMutableDictionary dictionaryWithCapacity:[edits count]]; ! 48: NSEnumerator *editEnum = [edits keyEnumerator]; ! 49: NSString *key; ! 50: NSDictionary *errors; ! 51: ! 52: while (key = [editEnum nextObject]) { ! 53: // construct a selector and see if the have a validation method ! 54: id value = [[controller dataSource] coerceValue:[edits objectForKey:key] forKey:key]; ! 55: if (!value) { ! 56: [self reportValidationError:@"Value entered is not of correct type" ! 57: forKey:key ofObject:object inController:controller]; ! 58: return nil; ! 59: } ! 60: ! 61: [converted setObject:value forKey:key]; ! 62: } ! 63: ! 64: errors = [object validateValuesInDictionary:converted]; ! 65: if([errors count]) { ! 66: [self reportValidationErrors:errors forObject:object inController:controller]; ! 67: // [controller discardEdits]; // throw away the bad values ! 68: return nil; ! 69: } ! 70: return converted; ! 71: } ! 72: return edits; ! 73: } ! 74: ! 75: ! 76: - (EODataSourceOperationDelegateResponse)controller:(EOController *)controller ! 77: willInsertObject:object ! 78: inDataSource:dataSource ! 79: { ! 80: if ([object respondsToSelector:@selector(validForInsertInDataSource:)]) { ! 81: NSDictionary *errors = [object validForInsertInDataSource:dataSource]; ! 82: if (errors) { ! 83: [self reportValidationErrors:errors forObject:object inController:controller]; ! 84: return EORollbackDataSourceOperation; ! 85: } ! 86: } ! 87: return EOPerformDataSourceOperation; ! 88: } ! 89: ! 90: - (EODataSourceOperationDelegateResponse)controller:(EOController *)controller ! 91: willUpdateObject:object ! 92: inDataSource:dataSource ! 93: { ! 94: if ([object respondsToSelector:@selector(validForUpdateInDataSource:)]) { ! 95: NSDictionary *errors = [object validForUpdateInDataSource:dataSource]; ! 96: if (errors) { ! 97: [self reportValidationErrors:errors forObject:object inController:controller]; ! 98: return EORollbackDataSourceOperation; ! 99: } ! 100: } ! 101: return EOPerformDataSourceOperation; ! 102: } ! 103: ! 104: - (EODataSourceOperationDelegateResponse)controller:(EOController *)controller ! 105: willDeleteObject:object ! 106: inDataSource:dataSource ! 107: { ! 108: if ([object respondsToSelector:@selector(validForDeleteInDataSource:)]) { ! 109: NSDictionary *errors = [object validForDeleteInDataSource:dataSource]; ! 110: if (errors) { ! 111: [self reportValidationErrors:errors forObject:object inController:controller]; ! 112: return EORollbackDataSourceOperation; ! 113: } ! 114: } ! 115: return EOPerformDataSourceOperation; ! 116: } ! 117: @end ! 118: ! 119: ! 120: @implementation NSObject (ValidationProtocols) ! 121: // An EO can implement some or all of these methods to play a roll in its validation ! 122: - (NSArray *)keysToValidate ! 123: { ! 124: return [NSArray array]; ! 125: } ! 126: ! 127: - (NSDictionary *)validForDataSource:(id <EODataSources>)dataSource; ! 128: { ! 129: return [self validateValuesInDictionary:[self valuesForKeys:[self keysToValidate]]]; ! 130: } ! 131: ! 132: - (NSDictionary *)validForInsertInDataSource:(id <EODataSources>)dataSource; ! 133: { ! 134: return [self validForDataSource:dataSource]; ! 135: } ! 136: ! 137: - (NSDictionary *)validForUpdateInDataSource:(id <EODataSources>)dataSource; ! 138: { ! 139: return [self validForDataSource:dataSource]; ! 140: } ! 141: ! 142: - (NSDictionary *)validForDeleteInDataSource:(id <EODataSources>)dataSource; ! 143: { ! 144: return nil; ! 145: } ! 146: @end
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.