Re: binding a form value to a NSTimestamp field
Re: binding a form value to a NSTimestamp field
- Subject: Re: binding a form value to a NSTimestamp field
- From: WebObjects <email@hidden>
- Date: Tue, 18 Apr 2006 15:17:13 -0700
- Thread-topic: binding a form value to a NSTimestamp field
Galen,
I considered javascript first thinking it would make things simpler, but
as it turns out I LOVE the idea of overriding
validationFailedWithException() - it works wonderfully! (simply)
If the format of the value entered isn¹t exactly parsable as m/%d/%y, then
it fails, I catch it, I do 'stuff' and return null (same component)
displaying their mistake. It's brilliant and I love it! I do like the
jscript below however, and will be cannibalizing it for other purposes ;-)
(hope you don't mind).
Elated, -Bill
on 4/18/06 14:55, Galen Rhodes at email@hidden wrote:
> Is Javascript an option? What about binding javascript event handlers to
> force them to enter only a properly formatted string. Given an input field
> defined in HTML like this:
>
> <input type=text size=12 maxlength=12 onblur="handleDateBlur(this)"
> onfocus="handleDateFocus(this)" onkeypress="handleDateKeypress(event, this)">
>
> And Javascript like so...
>
> //============================================================================
> ==========================
> // Get the key pressed from the even object.
> //============================================================================
> ==========================
> function getkey(e) {
> if(window.event) {
> return window.event.keyCode;
> }
> else if(e) {
> return e.which;
> }
> else {
> return null;
> }
> }
>
> //============================================================================
> ==========================
> // Strip all non-numeric characters out of a string.
> //============================================================================
> ==========================
> function numbersOnly(str) {
> var newStr = "";
> var i;
> var ch;
>
> for(i = 0; i < str.length; i++) {
> ch = str.charAt(i);
> if("0123456789".indexOf(ch) != -1) {
> newStr += ch;
> }
> }
> return newStr;
> }
>
> //============================================================================
> ==========================
> // Return the minimum of two numbers.
> //============================================================================
> ==========================
> function min(a, b) { return ((a < b)?(a):(b)); }
>
> //============================================================================
> ==========================
> // Format the date field as they're typing.
> //============================================================================
> ==========================
> function handleDateKeypress(e, fld) {
> var s = numbersOnly(fld.value);
> var key = getkey(e);
>
> if(key == null || key == 0 || key == 8 || key == 9 || key == 13 || key == 27)
> {
> if(key == 0 || key == 9) {
> return true;
> }
> if(key == 8 && s.length > 0) {
> s = s.substring(0, s.length - 1);
> }
> }
> else {
> var keychar = String.fromCharCode(key);
> if("0123456789".indexOf(keychar) != -1) {
> s = s + keychar;
> }
> }
> formatDateString(fld, s, true);
> return false;
> }
>
> //============================================================================
> ==========================
> // Handle Date Focus
> //============================================================================
> ==========================
>
> function handleDateFocus(fld) {
> formatDateString(fld, numbersOnly(fld.value), true);
> }
>
> function handleDateBlur(fld) {
> formatDateString(fld, numbersOnly(fld.value), false);
> }
>
> function formatDateString(fld, s, b) {
> var newval = "";
> if(s.length > 0) {
> newval = s.substring(0, min(2, s.length));
> }
> if(s.length > 2) {
> newval = newval + "/" + s.substring(2, min(4, s.length));
> }
> if(s.length > 4) {
> newval = newval + "/" + s.substring(4, min(8, s.length));
> }
> if(b && newval.length < 10) {
> newval = newval + "mm/dd/yyyy".substring(newval.length, 10);
> }
> fld.value = newval;
> if(b) fld.select();
> }
>
>
>
> On Apr 18, 2006, at 11:55 AM, WebObjects wrote:
>
>> There's always an easier and better way, so why not ask? (right?)
>>
>> I have a field in my form for collecting someone's date-of-birth. The value
>> will store as an NSTimestamp in the dbase.
>>
>> public void setDtDob(NSTimestamp value) {
>> takeStoredValueForKey(value, "dtDob");
>> }
>>
>> Q: What is the best way to validate the users input, or get it to conform
>> to what NSTimestamp will accept for a valid date format?
>>
>> I have the field formatted like so:
>>
>> TextField5: WOTextField {
>> dateformat = "%m/%d/%y";
>> value = newDonor.dtDob;
>> }
>>
>> ...however, this does nothing to protect me from a user who insists in
>> typing gobbly-goop into the field. I suppose a series of dropdown elements
>> could help enforce input, but I'd rather use a way of catching the exception
>> (here is the exception when user-input cannot be parsed):
>>
>> [2006-04-18 08:19:52 PDT] <WorkerThread12> Validation failed on an object
>> [java.lang.String] with keypath = newDonor.dtDob and exception:
>> Format.parseObject(String) failed
>>
>> Does anyone have a good method written for testing the String in a field
>> against a formatting (ie. "%m/%d/%y") ?
>>
>> TIA,
>> -Bill
>>
>>
>> _______________________________________________
>> Do not post admin requests to the list. They will be ignored.
>> Webobjects-dev mailing list (email@hidden)
>> Help/Unsubscribe/Update your Subscription:
>> d.com
>>
>> This email sent to email@hidden
>>
>>
>
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden