Re: "final" string validation in custom NSFormatter
Re: "final" string validation in custom NSFormatter
- Subject: Re: "final" string validation in custom NSFormatter
- From: "Louis C. Sacha" <email@hidden>
- Date: Sun, 12 Dec 2004 01:51:29 -0800
Hello...
Yes, you would do your final validation in the
getObjectValue:forString:errorDescription: method. If the provided
string is incomplete or invalid, you should return FALSE from that
method to prevent the end of editing.
Generally, the formatter would do any necessary conversion or
modification in the getObjectValue:forString:errorDescription:
method, and the stringForObjectValue: method simply creates a single
default format for the string.
For example, if your formatter uses NSNumber as it's object type, the
getObjectValue:forString:errorDescription: method would be
responsible for parsing the input string and determining if the input
is a fraction or decimal and supplying an NSNumber instance with the
correct value. The stringForObjectValue: method would just take the
NSNumber and return a string using a standard format.
When the user finishes editing the string in the text field (or
whatever other control), the object value is requested from the
formatter using getObjectValue:forString:errorDescription:, and then
that object value is passed back through the stringForObjectValue:
method and the resulting string is used as the new contents of the
text field. So the updated content of the text field after editing
will always be in the format provided by your stringForObjectValue:
method, no matter what form it was entered in.
Hope that helps,
Louis
When writing a custom formatter, how do I do a "final" string
validation? I've overriden "isPartialStringValid", which works
fine, up to a point.
In my case, I allow fractions. So the user typing in "1/" is valid
to that point. However, if they try to commit that (pressing the
enter key, tabbing, exiting out, etc) the string is now INVALID
(i.e., you need a denominator).
I assume there should be some test in getObjectValue for "exit key",
returning an error (not allowing the user to exit) if the final keys
are not valid, and a similar test in stringForObjectValue that would
not cause a reformat until the text field was committed & validated
(i.e., I might allow fractional input, but want only decimal output,
1/2 --> 0.5).
Thanks for any insight!
mark
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden