2014-09-16 2 views
3

"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"와 같은 "비밀"이 개 속성에 추가됩니다

- (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:@"[email protected]"]; 
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait]; 

그것은 저장 실패 사람이 엔티티가 유효한 암호 유효성 검증을 통과하지 못하고 암호 필드를 확인하지 않는다고 말하는 상황. Facebook에서 가입하는 동안 비밀번호를 입력하거나 비밀번호 입력란을 확인할 필요가 없습니다. "password"및 "confirmPassword"값을 저장하지 않고 컨텍스트를 저장하려면 어떻게해야합니까?

+0

비밀번호 및 confirmPassword 필수 입력란입니까, 아니면 선택 사항입니까? – casademora

+0

두 필드 모두 선택 사항입니다 ... –

답변

1

그것은 매우 간단 보인다

  • 클래스 PersonpasswordconfirmPassword 필드에 대한 검증 방법을 포함한다. 이 메소드는 nil 값을 허용하지 않습니다.
  • Person의 인스턴스를 만들었지 만 password의 값을 설정하지 않았습니다. 따라서 새 인스턴스에는이 필드에 대해 nil 값이 있습니다.
  • 변경 내용을 저장하려고 시도했습니다.

간단히 말해서 고객님의 인증 코드가을 실패해야하므로 인증에 실패했습니다. 확인을 통과하려면 password (유효한 값은 코드에서 허용 할 수 있음)에 유효한 값을 할당하거나 유효성 검사 코드를 변경하여 nil 값이 유효성 검사를 통과해야합니다.

Facebook이이 질문과 어떤 관련이 있는지 분명하지 않습니다. 귀하의 코드에있는 어떤 것도 Facebook과 관련이없는 것으로 보입니다.