Permalink
Browse files

Added validation blocks to survey field

  • Loading branch information...
1 parent fce8831 commit bed5f1f40e25ccdbeddd4defb2cea9e2a06e4f91 @wess committed Oct 17, 2012
View
@@ -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;
}
@@ -8,17 +8,20 @@
#import <Foundation/Foundation.h>
+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;
@@ -15,7 +15,8 @@ - (id)init
self = [super init];
if(self)
{
- _isRequired = NO;
+ _isRequired = NO;
+ _validationBlock = NULL;
}
return self;
}
@@ -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)
@@ -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 */
};
@@ -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;
}
View
BIN hJHYq.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit bed5f1f

Please sign in to comment.