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!
Asked
Active
Viewed 158 times
1 Answers
1
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.

Rob Keniger
- 45,830
- 6
- 101
- 134
-
While I agree that this is the most logical solution, my goal was to ask the user (via a dialog) which modified value they wanted to save before the application terminated. I overrode... -(void)canCloseDocumentWithDelegate:shouldCloseSelector:contextInfo: in MyDocument and was able to put up the dialog box I wanted. However, the terminate command aborted and I was left with the app unable to quit (Quit menu item was disabled). Someone with more knowledge than me can probably explain why. – Scott Henderson Apr 24 '12 at 01:10
-
At this point I have the app observing the NSManagedObjectContextWillSaveNotification and, if the user entered value is invalid, the Entity's attribute is set back to the original (valid) value. – Scott Henderson Apr 24 '12 at 01:11