Re: Visual feedback of invalid cell data from Core Data - how to?
Re: Visual feedback of invalid cell data from Core Data - how to?
- Subject: Re: Visual feedback of invalid cell data from Core Data - how to?
- From: Andreas Grosam <email@hidden>
- Date: Sun, 20 Mar 2011 12:40:59 +0100
On Mar 19, 2011, at 9:44 PM, Luke Evans wrote:
> I'm curious as to the best (or a) way to achieve the following:
>
> - I have a Core Data model set up with validation of various kinds
> (non-optional values, ranges, regexs etc.) on attributes.
> - I have a table based UI showing these entities for viewing and editing.
>
> I would like to make the background colour of my table cells change colour
> (maybe to yellow or red) if the value therein does not validate, according
> to the Core Data constraints of course.
>
> Now, it's easy enough to set up the basic validation protocol, but this
> seems to assume you want to put up an error of some kind (i.e. a sheet) when
> validation fails.
When you invoke -save: for the context the validation methods will be automatically invoked by Core Data. :save returns a boolean indicating the result of the operation. I don't think Core Data makes any assumptions about what you are doing with the result and with the possible returned error object.
> What I _think_ I'd like is for the regular binding validation process,
> perhaps even with "Validates immediately", to somehow get state into my
> (presumably custom) cell telling it the value is invalid so I can set a
> visual cue.
>
> I definitely want the regular Core Data validation process to propagate and
> drive this, rather than having to set formatters on all my cell (or
> something) and manually figure out validation by some means.
You can invoke at *any time* the managed object's validation methods. Core Data will only do this immediately before it actually commits the context when -save: has been sent to it.
There are two types of validation methods:
1) property-level validation
2) inter-object validation
The first group handles validation on property scope. Suppose you have a property 'myProperty'. You usually define a method
- (BOOL) validateMyProperty(id*) ioValue error:(NSError**)outError;
in order to perform additional validations not covered by Core Data itself which is actually performed in -validateValue:forKey:error:. You shall not override -validateValue:forKey:error: in order to perform your custom property validation.
You do not invoke super.
Important also: you can invoke property level validation methods at any time, but you shall not invoke this method *directly* - instead you must invoke -validateValue:forKey:error: with the corresponding key, e.g. @"myProperty". This ensure that basic validations are performed by Core Data itself. You custom validation method will be invoked by -validateValue:forKey:error:.
The second group, inter-object validation comprises the methods:
- (BOOL)validateForInsert:(NSError **)error;
- (BOOL)validateForUpdate:(NSError **)error;
- (BOOL)validateForDelete:(NSError **)error;
When you override it in your managed object class, you shall call super first. The invocation of super will also validate each property by invoking the corresponding validation method and also performs the referential integrity checks defined in the model.
The error should return a list of errors for each validation. (Somewhere in the doc or in an example there is code which shows how you accumulate validation errors).
As stated (in the docs), you can call these methods at any time.
>
> Is there a slick way to do this, or is does this sort of thing require a lot
> of work to introduce custom stringy bits to get per-cell validation?
Having said this, the solution to your problem may become straight forward, although I haven't done exactly this myself yet (on Mac OS X). So far, I just save the context and handle errors accordingly at save time using an "editingContext", save it frequently, and when it succeeds merge this context into the main context. Your solution strives to become more user-friendly and I like the idea. You need to figure out when you trigger the validation method (and which) and then process the error if there is any.
Regards
Andreas
>
> Thanks!
>
> -- Luke
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden