Scott Henderson
Scott Henderson

Reputation: 101

Intercepting Save in Core Data Document

I have a window with a small text box, bound to a Core Data attribute. The value the user enters into the text box needs to be within certain parameters. These parameters are dynamic. If the entered value is outside of the parameters, a dialog box is displayed asking if the user wants to revert to a previous value, set the value to a minimum, etc. I have implemented the controlTextDidEndEditing method to intercept and validate the value the user enters. My problem is when the user saves or quits. The user can enter a bad value, select save or quit and value is saved, bypassing the validation. Is there a way to have my validation method called before a save? Thanks!

Upvotes: 0

Views: 163

Answers (1)

Rob Keniger
Rob Keniger

Reputation: 46028

Instead of using the text field delegate, you should instead implement validation in your NSManagedObject subclasses. You can then enforce what values are valid and return an appropriate error message if an invalid value is entered. Doing it this way means that the model is responsible for enforcing validity, which is the logical place to do it.

There is more info about validation in the appropriate section of the Core Data documentation.

Upvotes: 1

Related Questions