Re: Core-Data & validate<key>:error:
Re: Core-Data & validate<key>:error:
- Subject: Re: Core-Data & validate<key>:error:
- From: Kaspar Fischer <email@hidden>
- Date: Wed, 7 Sep 2005 10:03:27 +0200
On 07.09.2005, at 02:27, mmalcolm crawford wrote:
On Sep 6, 2005, at 10:27 AM, Kaspar Fischer wrote:
I implemented validate<key>:error: for an attribute of
a Core-Data entity. It works in the sense that when
I change the attribute's value and save the document,
validation takes place.
However, when the entity is created (with an illegal
value, say) then *no* validation takes place when I
save the document. Is this behaviour intentional,
that is, are newly created entities not subject to
validation?
Validation is by default performed in all these situations. Have
you overridden, say, validateForInsert:, and not invoked the
superclass's implementation?
Thanks, mmalcolm, for your answer.
You are right, validation is indeed performed! But my
returning NO is ignored. More precisely, my routine
looks as follows:
- (BOOL)validateValue:(id *)value error:(NSError **)error
{
NSLog(@"validateValue %@",*value);
if (*value == nil)
return YES;
id desc = [self valueForKey:@"attributeDescription"];
if (![[desc valueForKey:@"allowFromListOnly"] boolValue]) {
NSNumber *min = [desc valueForKey:@"minValue"];
NSNumber *max = [desc valueForKey:@"maxValue"];
NSLog(@"validateValue called min %f max %f value %f",[min
floatValue],[max floatValue],[*value floatValue]);
if (min != nil && [min floatValue] > [*value floatValue]) {
NSDictionary *userInfo = [NSDictionary
dictionaryWithObjectsAndKeys:
@"valueIfNotItemize", NSValidationKeyErrorKey,
*value, NSValidationValueErrorKey,
self, NSValidationObjectErrorKey, nil];
*error = [[NSError errorWithDomain:NSCocoaErrorDomain
code:NSValidationNumberTooSmallError
userInfo:userInfo] retain];
NSLog(@"min");
return NO;
}
if (max != nil && [max floatValue] < [*value floatValue]) {
*error = [NSError errorWithDomain:NSCocoaErrorDomain
code:NSValidationNumberTooLargeError
userInfo:nil];
NSLog(@"max");
return NO;
}
}
return YES;
}
and the output in the log is
2005-09-07 09:56:26.541 App[19043] validateValue -10
2005-09-07 09:56:26.541 App[19043] validateValue called min 0.000000
max 10.000000 value -10.000000
2005-09-07 09:56:26.541 App[19043] min
So my routine returns NO, yet the error is not displayed!
What could be the reason for this? (It works as soon as
I manually enter "0" first and then "-10" again...)
Thanks,
Kaspar
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden