diff --git a/README.md b/README.md index e908391..3799c61 100644 --- a/README.md +++ b/README.md @@ -27,9 +27,13 @@ @implementation RegisterForm -(SurveyField *)firstname { - SurveyField *field = [SurveyField fieldWithPlaceholder:@"First Name"]; - field.isRequired = NO; - field.label = @"First Name"; + SurveyField *field = [SurveyField fieldWithPlaceholder:@"First Name"]; + field.isRequired = NO; + field.label = @"First Name"; + field.validationBlock = ^(id form, id field, id value) { + NSString *fieldValue = [(NSString *)value lowercaseString]; + return [fieldValue isEqualToString:@"wess"]; + }; return field; } diff --git a/Survey/Classes/SurveyField.h b/Survey/Classes/SurveyField.h index 28b5042..87c0c12 100644 --- a/Survey/Classes/SurveyField.h +++ b/Survey/Classes/SurveyField.h @@ -8,17 +8,20 @@ #import +typedef BOOL(^SurveyValidationBlock)(id form, id field, id value); + @interface SurveyField : NSObject -@property (copy, nonatomic) NSString *entityName; -@property (copy, nonatomic) NSString *label; -@property (copy, nonatomic) NSString *placeholder; -@property (copy, nonatomic) NSString *value; -@property (strong, nonatomic) Class fieldClass; -@property (strong, nonatomic) UITextField *field; -@property (strong, nonatomic) NSRegularExpression *expression; -@property (strong, nonatomic) NSString *errorMessage; -@property (readwrite, nonatomic) BOOL isRequired; -@property (readwrite, nonatomic) BOOL isSecure; +@property (copy, nonatomic) NSString *entityName; +@property (copy, nonatomic) NSString *label; +@property (copy, nonatomic) NSString *placeholder; +@property (copy, nonatomic) NSString *value; +@property (strong, nonatomic) UITextField *field; +@property (strong, nonatomic) NSRegularExpression *expression; +@property (strong, nonatomic) NSString *errorMessage; +@property (strong, nonatomic) Class fieldClass; +@property (strong, nonatomic) SurveyValidationBlock validationBlock; +@property (readwrite, nonatomic) BOOL isRequired; +@property (readwrite, nonatomic) BOOL isSecure; + (SurveyField *)fieldWithPlaceholder:(NSString *)placeholder; diff --git a/Survey/Classes/SurveyField.m b/Survey/Classes/SurveyField.m index 005ef48..b96166a 100644 --- a/Survey/Classes/SurveyField.m +++ b/Survey/Classes/SurveyField.m @@ -15,7 +15,8 @@ - (id)init self = [super init]; if(self) { - _isRequired = NO; + _isRequired = NO; + _validationBlock = NULL; } return self; } diff --git a/Survey/Classes/SurveyForm.m b/Survey/Classes/SurveyForm.m index 7884df1..9445e84 100644 --- a/Survey/Classes/SurveyForm.m +++ b/Survey/Classes/SurveyForm.m @@ -199,6 +199,18 @@ - (void)validateForm [mutableFieldErrors addObject:matchError]; } + if(fieldObject.validationBlock != NULL) + { + BOOL isValid = fieldObject.validationBlock(self, fieldObject.field, value); + if(!isValid) + { + _fieldsAreValid = NO; + + NSString *matchError = (fieldObject.errorMessage)? fieldObject.errorMessage : @"Validation failed"; + [mutableFieldErrors addObject:matchError]; + } + } + [_fieldValues setObject:((value)? value : @"") forKey:fieldObject.entityName]; if(mutableFieldErrors.count > 0) diff --git a/SurveyExample/SurveyExample.xcodeproj/project.pbxproj b/SurveyExample/SurveyExample.xcodeproj/project.pbxproj index 9641127..df76666 100644 --- a/SurveyExample/SurveyExample.xcodeproj/project.pbxproj +++ b/SurveyExample/SurveyExample.xcodeproj/project.pbxproj @@ -284,10 +284,7 @@ GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "SurveyExample/SurveyExample-Prefix.pch"; INFOPLIST_FILE = "SurveyExample/SurveyExample-Info.plist"; - LIBRARY_SEARCH_PATHS = ( - "$(inherited)", - "\"$(SRCROOT)/../../../Library/Developer/Xcode/DerivedData/Survey-efmxnwsndofvtxbkslugynnmuymv/Build/Products/Debug-iphoneos\"", - ); + LIBRARY_SEARCH_PATHS = "$(inherited)"; PRODUCT_NAME = "$(TARGET_NAME)"; WRAPPER_EXTENSION = app; }; @@ -299,10 +296,7 @@ GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "SurveyExample/SurveyExample-Prefix.pch"; INFOPLIST_FILE = "SurveyExample/SurveyExample-Info.plist"; - LIBRARY_SEARCH_PATHS = ( - "$(inherited)", - "\"$(SRCROOT)/../../../Library/Developer/Xcode/DerivedData/Survey-efmxnwsndofvtxbkslugynnmuymv/Build/Products/Debug-iphoneos\"", - ); + LIBRARY_SEARCH_PATHS = "$(inherited)"; PRODUCT_NAME = "$(TARGET_NAME)"; WRAPPER_EXTENSION = app; }; @@ -327,6 +321,7 @@ 08C867F7162CE99200B98C14 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/SurveyExample/SurveyExample/WCRegisterForm.m b/SurveyExample/SurveyExample/WCRegisterForm.m index c8b2009..4e0dd42 100644 --- a/SurveyExample/SurveyExample/WCRegisterForm.m +++ b/SurveyExample/SurveyExample/WCRegisterForm.m @@ -22,6 +22,12 @@ - (SurveyField *)lastname SurveyField *field = [SurveyField fieldWithPlaceholder:@"Last Name"]; field.isRequired = YES; field.label = @"Last Name"; + field.validationBlock = ^(id form, id field, id value) { + NSString *fieldValue = [(NSString *)value lowercaseString]; + NSLog(@"FIELD VALUE: %@", fieldValue); + + return [fieldValue isEqualToString:@"cope"]; + }; return field; } diff --git a/hJHYq.png b/hJHYq.png new file mode 100644 index 0000000..d0f1a5b Binary files /dev/null and b/hJHYq.png differ