Core data 自定义验证期间Magic record核心数据中的上下文保存问题

Core data 自定义验证期间Magic record核心数据中的上下文保存问题,core-data,ios7,nsmanagedobjectcontext,magicalrecord,Core Data,Ios7,Nsmanagedobjectcontext,Magicalrecord,我已经创建了一个名为“Person”的实体。 以下是实体的属性 @property (nonatomic, retain) NSString * address; @property (nonatomic, retain) NSString * confirmPassword; @property (nonatomic, retain) NSString * createdOn; @property (nonatomic, retain) NSString * email; @property

我已经创建了一个名为“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”上添加自定义验证,如:


它无法保存上下文,说明person实体未通过有效的密码验证和确认密码字段。在从Facebook注册期间,我不需要输入密码和确认密码字段。在不保存“password”和“confirmPassword”的值的情况下,我应该如何保存上下文?

这似乎很简单:

  • 您的类
    Person
    包括
    password
    confirmPassword
    字段的验证方法。这些方法不接受nil值
  • 您创建了
    Person
    的实例,但没有为
    密码设置值。因此,此字段的新实例具有nil值
  • 您试图保存更改
简而言之,验证失败是因为您自己的验证代码要求它失败。要通过验证,您必须为
密码
(其中“valid”表示代码将接受的内容)指定一个有效值,或者更改验证代码以使nil值通过验证


目前还不清楚Facebook与这个问题有什么关系。您的代码中似乎没有任何内容与Facebook有任何关联

password和confirmPassword是必填字段还是可选字段?这两个字段都是可选字段。。。
- (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  *userProfile = [Person MR_createEntity];
NSString *facebookId = @"some id";
[userProfile setFbId:facebookId];
[userProfile setEmail:@"umairsuraj.engineer@gmail.com"];
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];