mirror of
https://github.com/zhigang1992/RETableViewManager.git
synced 2026-05-22 19:09:20 +08:00
Add inline validation example
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
@property (strong, readwrite, nonatomic) RETextItem *textItem;
|
||||
@property (strong, readwrite, nonatomic) RETextItem *emailItem;
|
||||
@property (strong, readwrite, nonatomic) RETextItem *urlItem;
|
||||
@property (strong, readwrite, nonatomic) RETextItem *inlineTestItem;
|
||||
|
||||
@end
|
||||
|
||||
@@ -47,9 +48,22 @@
|
||||
self.urlItem = [RETextItem itemWithTitle:@"URL" value:@"http://invalid-url.co%m" placeholder:@"URL item"];
|
||||
self.urlItem.validators = @[@"url"];
|
||||
|
||||
// Inline Validation Example
|
||||
//
|
||||
REValidator *nameValidator = [REValidator validator];
|
||||
nameValidator.inlineValidation = ^NSError *(NSString *string, NSString *name) {
|
||||
if ([string componentsSeparatedByString:@" "].count < 2) {
|
||||
return [NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedDescriptionKey: @"Please enter first and last name."}];
|
||||
}
|
||||
return nil;
|
||||
};
|
||||
self.inlineTestItem = [RETextItem itemWithTitle:@"Name" value:@"" placeholder:@"First & Last Name"];
|
||||
self.inlineTestItem.validators = @[nameValidator];
|
||||
|
||||
[section addItem:self.textItem];
|
||||
[section addItem:self.emailItem];
|
||||
[section addItem:self.urlItem];
|
||||
[section addItem:self.inlineTestItem];
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
|
||||
@@ -41,7 +41,7 @@
|
||||
dispatch_once(&onceToken, ^{
|
||||
_sharedClient = [[REValidation alloc] init];
|
||||
});
|
||||
|
||||
|
||||
return _sharedClient;
|
||||
}
|
||||
|
||||
@@ -111,18 +111,23 @@
|
||||
+ (NSArray *)validateObject:(NSObject *)object name:(NSString *)name validators:(NSArray *)validators
|
||||
{
|
||||
NSMutableArray *errors = [NSMutableArray array];
|
||||
|
||||
|
||||
for (id validator in validators) {
|
||||
NSError *error;
|
||||
if ([validator isKindOfClass:[NSString class]]) {
|
||||
error = [self validateObject:object name:name validatorString:(NSString *)validator];
|
||||
} else {
|
||||
error = [self validateObject:object name:name validator:validator];
|
||||
REValidator *v = (REValidator *)validator;
|
||||
if (v.inlineValidation) {
|
||||
error = [[v class] validateObject:object variableName:name validation:v.inlineValidation];
|
||||
} else {
|
||||
error = [self validateObject:object name:name validator:validator];
|
||||
}
|
||||
}
|
||||
if (error)
|
||||
[errors addObject:error];
|
||||
}
|
||||
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
@@ -132,9 +137,9 @@
|
||||
self = [super init];
|
||||
if (!self)
|
||||
return nil;
|
||||
|
||||
|
||||
self.registeredValidators = [[NSMutableDictionary alloc] init];
|
||||
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
@interface REValidator : NSObject
|
||||
|
||||
@property (strong, readonly, nonatomic) NSDictionary *parameters;
|
||||
@property (copy, readwrite, nonatomic) NSError *(^inlineValidation)(id object, NSString *name);
|
||||
|
||||
///-----------------------------
|
||||
/// @name Getting Validator Instance
|
||||
@@ -48,5 +49,6 @@
|
||||
///-----------------------------
|
||||
|
||||
+ (NSError *)validateObject:(NSObject *)object variableName:(NSString *)name parameters:(NSDictionary *)parameters;
|
||||
+ (NSError *)validateObject:(NSObject *)object variableName:(NSString *)name validation:(NSError *(^)(id object, NSString *name))validation;
|
||||
|
||||
@end
|
||||
|
||||
@@ -55,6 +55,13 @@
|
||||
return nil;
|
||||
}
|
||||
|
||||
+ (NSError *)validateObject:(NSObject *)object variableName:(NSString *)name validation:(NSError *(^)(id object, NSString *name))validation
|
||||
{
|
||||
if (validation)
|
||||
return validation(object, name);
|
||||
return nil;
|
||||
}
|
||||
|
||||
+ (NSDictionary *)parseParameterString:(NSString *)string
|
||||
{
|
||||
return nil;
|
||||
|
||||
Reference in New Issue
Block a user