Problem with preserving context in Magic Record Master Data during spot check

I have created an object named "Face". Following are the attributes of the object.

@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;

      

Custom validation is added by two attributes: "confirmPassword" and "password":

- (BOOL)validatePassword:(id *)ioValue error:(NSError **)outError {

    // Password validation is not specified in the model editor, it 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 validation is not specified in the model editor, it 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;
}

      

The values ​​are stored in the Person object, for example:

Person  *userProfile = [Person MR_createEntity];
NSString *facebookId = @"some id";
[userProfile setFbId:facebookId];
[userProfile setEmail:@"umairsuraj.engineer@gmail.com"];
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];

      

Unable to save context saying that the human entity does not pass valid password validation and validate the password fields. I don't need to enter password and confirm password fields during Facebook registration. what should i do to save the context without saving values ​​for "password" and "confirmPassword".?

+3


source to share


1 answer


It looks pretty simple:

  • Your class Person

    includes validation methods for the password

    and fields confirmPassword

    . These methods do not accept nil values.
  • You instantiated Person

    but did not set a value for password

    . Therefore, the new instance is nil for this field.
  • You are trying to save your changes.


In short, validation fails because your own validation code requires it to fail . Validation requires you to provide a valid value for password

(where "valid" means something your code will accept) or change the validation code so that nil passes the validation.

It is unclear what Facebook is supposed to do with this issue. Nothing in your code has anything to do with Facebook.

+1


source







All Articles