Custom alerts for Core Data validation errors ... how to?

  • Guys,

    I have kind of an extremely dumb question ... yet despite my
    Googling, I cannot find an answer to it.

    I have a Core Data application and I have used the data model to do
    various validations ... mostly regular expression matches on
    strings.  It works great, but I'd like to customize the validation
    alerts that are generated when validation fails.  For example, if a
    typical validation fails, the "standard" alert says something like:

        Shard secret is invalid.

    But I'd like to change that to something like this:

        The string you have entered does not appear to be a shared secret.

        A shared secret must be exactly 32 characters long and contain
        only letters and numbers. Discard changes or try again?

    "Normally" I'd be able to provide my own validation methods and craft
    the NSErrors ... but this is Core Data ... and the object model is
    doing the validation for me.  I'm happy with the way the object model
    is doing validation ... I just want to customize the error
    descriptions, recovery suggestions, and recovery options in the
    validation alert by way of my own NSErrors.  The NSPersistentDocument
    tutorial shows a way to NSDocuments -willPresentError delegate method
    to present a custom alert ... I -could- go that route ... but then
    I'd be doing jillions of tests ("is it this error? ... No, okay, this
    error? ... etc.).  I basically want something that has the
    functionality of "OK, object model says this isn't valid ... so use
    this NSError in the alert that displays") within my managed object
    subclasses ... something like what validateSharedSecret:(id *)ss
    error:(NSError *)outError; does ... without tacking on the extra
    error if I return "NO".  Alternatively, I'll be happy to somehow
    validate using a regular expression within my own validator (i.e.,
    let my custom validator do the work rather than the data model).

    Does this make any kind of sense?
  • On Nov 30, 2007, at 2:33 AM, Richard Wolf wrote:

    > "Normally" I'd be able to provide my own validation methods and
    > craft the NSErrors ... but this is Core Data ... and the object
    > model is doing the validation for me.
    >
    Why not just implement your own managed object validation methods?

    mmalc
  • On Nov 30, 2007, at 4:24 AM, mmalc crawford wrote:

    >
    > On Nov 30, 2007, at 2:33 AM, Richard Wolf wrote:
    >
    >> "Normally" I'd be able to provide my own validation methods and
    >> craft the NSErrors ... but this is Core Data ... and the object
    >> model is doing the validation for me.
    >>
    > Why not just implement your own managed object validation methods?

    Well, I've thought about that, I really have.  Problem is this:

    In the data modeling tool in XCode, I have my entity defined nice and
    neat ... and the settings for my "sharedSecret" attribute are all
    laid out ... including the "Reg. Ex." text string ...

    [A-Z0-9]{32,32}

    I aspire to be a good, lazy programmer ... let the data modeling tool
    hang onto the regex if I can get away with it.  But if I do that, the
    only control I have over the error alert (short of doing a view/
    window/doc delegate) is how the attribute name appears ... I can go
    from "sharedSecret" to "Shared secret".

    Now I'm more than happy to move that regex string into a

    -(BOOL)validateSharedSecret:(id *)ioValue error:(NSError **)outError

    method in my NSManagedObject subclass.  But what I can't find is a
    way to do this:

    BOOL result = [*ioValue matchesRegularExpression:@"[A-Z0-9]{32,32}"];

    At least, so far as I know, there is no Apple-approved way to do
    that.  I could glom onto another Cocoa regex class, but I thought
    that ... somehow ... CoreData already knows how to do that ... so
    I'll do the work if I have to, but if there is some way to do it
    simply, I'd super-appreciative ("buy you a beer" appreciative) to
    know how.

    Thanks Malcolm!
  • On Nov 30, 2007, at 12:30 PM, Richard Wolf wrote:

    > But what I can't find is a way to do this:
    >
    > BOOL result = [*ioValue matchesRegularExpression:@"[A-Z0-9]{32,32}"];

    NSPredicate has a matches-regular-expression operator.

    That's how the validation conditions you define in your model are
    actually represented at runtime; see the documentation for
    NSPropertyDescription's validationPredicates property.

      -- Chris
  • On Nov 30, 2007, at 2:47 PM, Chris Hanson wrote:

    > NSPredicate has a matches-regular-expression operator.
    >
    > That's how the validation conditions you define in your model are
    > actually represented at runtime; see the documentation for
    > NSPropertyDescription's validationPredicates property.

    Now see, that's what I'm talkin' about! ... I -knew- there had to be
    a way.  :)

    Chris Hanson, if we should ever meet, say at WWDC, you may claim a
    free beer from me!  I would also buy Malcolm a beer because he is
    cool and because his stuff on the web has been extremely helpful.  :)
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today