Good morning,

I'm trying to implement some input validation on an NSTextField using 
NSControlTextEditingDelegate's -control:isValidObject: method -- simple stuff 
like validating that the entered text is parsable as a number, falls within a 
certain range, etc. (I know that NSNumberFormatter is the obvious choice for 
this scenario, but I'm working with requirements that require more low-level 
customizability, particularly in the content of the error message and how it is 
presented to the user, than NSNumberFormatter allows.) I've got things 
generally working; the delegate method gets called, I validate the user's entry 
and return NO if it is invalid, and the text field remains first responder.

The obstacle I've encountered is that in some cases my text field is a 
subordinate control that is enabled/disabled based on a checkbox elsewhere in 
my view. If the user enters something invalid and attempts to commit the edit 
by pressing tab/enter/return, I validate and display the error message and the 
text field remains first responder, as desired. However, if the user then 
unchecks the controlling checkbox -- causing the text field to be disabled via 
-setEnabled:NO in my view controller -- the text field is disabled but it keeps 
the invalid text.

At this point, the user has successfully circumvented my input validation; they 
can just re-check the checkbox and the text field is reenabled containing the 
invalid text. Worse, if the user puts key focus in the NSTextField again but 
does not make any edits and then tabs out, the validation is not triggered 
because there are no pending edits. I do note that if the user enters invalid 
text but does not attempt to commit the edit and then unchecks the checkbox, 
the NSTextField is disabled and the text reverts back to what it was before the 
user made their edit. It seems that even though I'm returning NO from 
-control:isValidObject:, the edit is still 'committed' from the field editor to 
the NSTextField. What I want is for the text field to revert back to the 
previous valid entry upon unchecking the box, even after the user attempts to 
commit the invalid edit and is presented with an error message.

So, it sounds like -control:isValidObject: might be too late in the pipeline to 
validate and prevent the edit from being committed in the first place. Any 
suggestions for an earlier point to hook in my validation? I did try 
-control:textShouldEndEditing:, but still see the same behavior. Or, a 
different approach to accomplish what I'm after?

Thanks!
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to