views:

97

answers:

2

I've created a model in Xcode, and for various attributes I have minimum & maximum values defined and that are enforced by coreData at runtime.

I'm using NSManagedObject validateValue:forKey:error to check for user input values.

I was a bit disappointed to see that the localized error message is not specific, and I get a "The operation couldn't be completed. (Cocoa error 1620)" for too small values and a "The operation couldn't be completed. (Cocoa error 1610)" for too large values.

It's written in the coreData documentation that "you can localize most aspects of a managed object model, including entity and property names and error messages". Unfortunately it's not very detailed on how you implement it for error messages.

So How do you customize error message ? How do you localize them ? Is it possible to customize it in a way where it will mentioned the reference value ex: "Value should be lower than %@" or "Value should be higher than %@" ?

A: 

To customize the errors as you want, you will need to implement custom NSManagedObject subclasses and then implement the validation methods for each attribute to return the errors you wish.

TechZen
the fact is that the validation routines are already existing and are quite generic, I don't wan't to customize each of the nsmanaged objects when I can define min max values straight in the xcode coredata model designer. Either I find a way to access these values, or I might end up redefining my owns by using custom min/max userinfo values for each attributes...
Xav
The errors themselves are defined in CocoaErrors.h. 1620 is NSValidationNumberTooSmallError and 1610 is NSValidationNumberTooLargeError. You could also make a dictionary to return the enumeration name. The numerical codes are supposed to be for programmers and textual errors for end users. That is why the textual errors often contain only vague reports of an error.
TechZen
+1  A: 

It is possible to customize the error messages. The Core Data documentation discusses it in a little bit of detail.

Marcus S. Zarra
Are you sure RTFM is a valuable answer ? specially when I mentioned in the question that the doc is not detailed in this area :)
Xav
RTFM is frequently the answer and since you did not link to what documentation *you* were working from it is difficult to guess what parts of it you have read and applied. In fact, re-reading your question I still don't see mention of what you have tried that didn't work.
Marcus S. Zarra
ok, many thanks Marcus, that will be all...
Xav