【发布时间】:2014-09-16 07:12:30
【问题描述】:
我创建了一个名为“Person”的实体。 以下是实体的属性。
@property (nonatomic, retain) NSString * address;
@property (nonatomic, retain) NSString * confirmPassword;
@property (nonatomic, retain) NSString * createdOn;
@property (nonatomic, retain) NSString * email;
@property (nonatomic, retain) NSString * fbId;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * password;
@property (nonatomic, retain) NSString * phNumber;
@property (nonatomic, retain) NSString * sessionToken;
自定义验证添加在两个属性“confirmPassword”和“password”上,如:
- (BOOL)validatePassword:(id *)ioValue error:(NSError **)outError {
// Password's validation is not specified in the model editor, it's specified here.
// field width: min 4, max 32
BOOL isValid = YES;
NSString *password = *ioValue;
NSString *errorMessage;
NSInteger code = 0;
if (password.length == 0) {
errorMessage = @"Please enter password.";
code = NSValidationMissingMandatoryPropertyError;
isValid = NO;
} else if (password.length < 4) {
errorMessage = @"Password can't be less than 4 characters.";
code = NSValidationStringTooLongError;
isValid = NO;
} else if (password.length > 32) {
errorMessage = @"Password can't be more than 32 characters.";
code = NSValidationStringTooLongError;
isValid = NO;
}
if (outError && errorMessage) {
NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : errorMessage };
NSError *error = [[NSError alloc] initWithDomain:kHAB
code:code
userInfo:userInfo];
*outError = error;
}
return isValid;
}
- (BOOL)validateConfirmPassword:(id *)ioValue error:(NSError **)outError {
// Confirm Password's validation is not specified in the model editor, it's specified here.
// field validation
BOOL isValid = YES;
NSString *confirmPassword = *ioValue;
NSString *errorMessage;
NSInteger code = 0;
if (![confirmPassword isEqualToString:self.password]) {
errorMessage = @"The passwords must match";
code = NSValidationStringPatternMatchingError;
isValid = NO;
}
if (outError && errorMessage) {
NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : errorMessage };
NSError *error = [[NSError alloc] initWithDomain:kHAB
code:code
userInfo:userInfo];
*outError = error;
}
return isValid;
}
值被保存在 Person 实体中,例如:
Person *userProfile = [Person MR_createEntity];
NSString *facebookId = @"some id";
[userProfile setFbId:facebookId];
[userProfile setEmail:@"umairsuraj.engineer@gmail.com"];
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
无法保存上下文,说明人员实体未通过有效的密码验证并确认密码字段。从 Facebook 注册期间,我不需要输入密码和确认密码字段。我应该怎么做才能保存上下文而不保存“密码”和“确认密码”的值?
【问题讨论】:
-
password 和 confirmPassword 是必填字段还是可选字段?
-
两个字段都是可选的...
标签: core-data ios7 nsmanagedobjectcontext magicalrecord