Thread Rating:
  • 442 Vote(s) - 2.86 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Setting a value to DateField by keyboard
09-27-2011, 02:15 PM,
#1
Setting a value to DateField by keyboard
Hi,

I used DateField as below, the date format is YYYYMMDD

Users want input the value by keyboard as displaying format, this is impossibe because DateField allows text with only DD/MM/YYYY format

How can other format that we wish be allowed?

Code:
{DateField
    value = {DateTime.date},
    format-spec =
        {proc {dt:DateTime, df:DateField}:String
            {return
                
                {format "%4d%02d%02d",
                    dt.info.year, dt.info.month, dt.info.day
                }
            }
        }
}





09-27-2011, 05:45 PM,
#2
RE: Setting a value to DateField by keyboard
I suspect that you should actually be looking at date-format-spec.

date-format-spec == DateFieldFormatSpec.iso seems to match your desired parse/format rules.

If you have more complicated needs, you can ignore that and set parse-spec in addition to format-spec, but that is certainly more work.
09-27-2011, 07:49 PM, (This post was last modified: 09-27-2011, 07:50 PM by alchimiste.)
#3
RE: Setting a value to DateField by keyboard
Thanks a lot..

I implement what users want with your help.

The modified code as below:
PHP Code:
{DateField
    value 
= {DateTime.date},
    
parse-spec={proc {value:String, require-four-digit-year?:booldf:DateField }:(date:DateTimeday-start-pos:intday-end-pos:intmonth-start-pos:intmonth-end-pos:intyear-start-pos:intyear-end-pos:int)
                   
def buf = {StringBuf value}
                   {if {
buf.find-string "/"} > -or {buf.find-string "-"} > -1 then
                       
{if buf.size != 10 then
                           
{return {DateTime}, 895603}
                       }
                       
def str = {buf.to-String}
                       
def d = {DateTime year={{str.substr 04}.to-int}, month={{str.substr 52}.to-int}, day={{str.substr 82}.to-int}}
                       {return 
d895603}
                       
                    else
                       {if 
buf.size != 8 then
                           
{return {DateTime}, 674603}
                       }
                       
def str = {buf.to-String}
                       
def d = {DateTime year={{str.substr 04}.to-int}, month={{str.substr 42}.to-int}, day={{str.substr 62}.to-int}}
                       {return 
d674603}
                   }
               },
    
    
format-spec =
        {
proc {dt:DateTimedf:DateField}:String
            
{return
                
                {
format "%4d%02d%02d"
                    
dt.info.yeardt.info.monthdt.info.day
                
}
            }
        }

09-28-2011, 02:11 PM,
#4
RE: Setting a value to DateField by keyboard
That's about right but I believe your indices are incorrect.

Though the documentation for parse-spec is not entirely detailed on this point, the indices to return are selection indices, not character indices. The difference being that selection indices are to the left of the character indices in the string. This means that, for a fixed format of YYYYMMDD, you would return (0,4)(4,6)(6,8) instead of (0,3)(4,5)(6,7). Plus you should still set date-format-spec -- though this is not clear at all from the documentation. (Also you should set it first so that it doesn't override your parse-spec and format-spec.) Probably the documentation could be improved here, and I will make a note of that.

Also I think some of your returned indices are incorrect even with the assumption of character indices. You can tell there's an issue by pressing up and down in the DateField; this should get you single year/month/day moves, but the selection is short by an index, so looks weird, and it can miss the desired change depending on where the caret index is.

I haven't extensively tested this, but I think this reflects what you were aiming for:

Code:
{curl 7.0, 8.0 applet}

{def df =
    {DateField
        value = {DateTime.date},
        date-format-spec = DateFieldFormatSpec.ymd
    }
}

{do
    set df.parse-spec =
        {proc {str:String,    
               require-four-digit-year?:bool,
               df:DateField
              }:(date:DateTime,
                 day-start-pos:int,
                 day-end-pos:int,
                 month-start-pos:int,
                 month-end-pos:int,
                 year-start-pos:int,
                 year-end-pos:int
                )
            {if {str.find-string "/"} > -1 or {str.find-string "-"} > -1 then
                {if str.size != 10 then
                    {return {DateTime}, 8, 10, 5, 7, 0, 4}
                }
                def d =
                    {DateTime
                        year = {{str.substr 0, 4}.to-int},
                        month = {{str.substr 5, 2}.to-int},
                        day = {{str.substr 8, 2}.to-int}
                    }
                {return d, 8, 10, 5, 7, 0, 4}
                
             else
                {if str.size != 8 then
                    {return {DateTime}, 6, 7, 5, 6, 0, 3}
                }
                def d =
                    {DateTime
                        year = {{str.substr 0, 4}.to-int},
                        month = {{str.substr 4, 2}.to-int},
                        day = {{str.substr 6, 2}.to-int}
                    }
                {return d, 6, 8, 4, 6, 0, 4}
            }
        }
    
    set df.format-spec =
        {proc {dt:DateTime, df:DateField}:String
            {return
                {format "%4d%02d%02d",
                    dt.info.year, dt.info.month, dt.info.day
                }
            }
        }
}

{value df}

Note that I changed the formatting somewhat and removed the extraneous StringBufs.

As a note to others who might use this code, like the original code sample, it doesn't properly handle all possible DateField.separators, or unbalanced separators. For production, I would want to look at improving that aspect of parsing.
09-28-2011, 06:06 PM,
#5
RE: Setting a value to DateField by keyboard
Thanks again...



When I read your reply first, I didn't understand what the selection that you said means.

I've found that I can change year, month and day by pressing UP or DOWN key in DateFiel, and so I understand what you said.

That's great and very interesting.

I hope the documentation of Curl IDE would be more detailed.






Possibly Related Threads...
Thread Author Replies Views Last Post
  DateField の ValueFinished の抑制 umemura 1 2,597 01-24-2013, 08:10 PM
Last Post: umemura
Forum Jump:


Users browsing this thread:
1 Guest(s)

MyBB SQL Error

MyBB has experienced an internal SQL error and cannot continue.

SQL Error:
1017 - Can't find file: 'mybb_threadviews' (errno: 2)
Query:
INSERT INTO mybb_threadviews (tid) VALUES('286')