Skip to content

Commit

Permalink
Added validation blocks to survey field
Browse files Browse the repository at this point in the history
  • Loading branch information
wess committed Oct 17, 2012
1 parent fce8831 commit bed5f1f
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 22 deletions.
10 changes: 7 additions & 3 deletions README.md
Expand Up @@ -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;
}
Expand Down
23 changes: 13 additions & 10 deletions Survey/Classes/SurveyField.h
Expand Up @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion Survey/Classes/SurveyField.m
Expand Up @@ -15,7 +15,8 @@ - (id)init
self = [super init];
if(self)
{
_isRequired = NO;
_isRequired = NO;
_validationBlock = NULL;
}
return self;
}
Expand Down
12 changes: 12 additions & 0 deletions Survey/Classes/SurveyForm.m
Expand Up @@ -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)
Expand Down
11 changes: 3 additions & 8 deletions SurveyExample/SurveyExample.xcodeproj/project.pbxproj
Expand Up @@ -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;
};
Expand All @@ -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;
};
Expand All @@ -327,6 +321,7 @@
08C867F7162CE99200B98C14 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
6 changes: 6 additions & 0 deletions SurveyExample/SurveyExample/WCRegisterForm.m
Expand Up @@ -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;
}
Expand Down
Binary file added 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.