Forum OpenACS Q&A: Re: ad_form and dates

Collapse
7: Re: ad_form and dates (response to 1)
Posted by Don Baccus on
Here's some code from dotLRN which I rewrote to use ad_form when fixing a bug (it originally didn't check to see if the term start date preceeded the term end date, and if not much of the rest of dotLRN got very confused.) dotlrn_term::edit takes the formbuilder date type and does get_property manually, if writing from scratch I'd suggest doing it the form using to_sql(linear_date) if a full timestamp's needed. For dates truncated to the day, the formbuilder date type doesn't provide particularly user-friendly support.

a form snippet (things like term_id removed for simplification):

ad_form -name edit_term -export term_pretty_name -select_query_name select_term_info -form {

    {start_date:date              {label "Start Date"}
                                  {format {MONTH DD YYYY}}}

    {end_date:date                {label "End Date"}
                                  {format {MONTH DD YYYY}}}

} -validate {
    {start_date
        { [template::util::date::compare $start_date $end_date] <= 0 }
        "The term must start before it ends"
    }
} -edit_data {

    dotlrn_term::edit \
        -term_id $term_id \
        -term_name $term_name \
        -term_year [string trim $term_year] \
        -start_date $start_date \
        -end_date $end_date

    ad_returnredirect $referer
    ad_script_abort
}

ad_return_template
And the select query:
            select term_name,
                   term_year,
                   to_char(start_date, 'YYYY MM DD') as start_date,
                   to_char(end_date, 'YYYY MM DD') as end_date
            from dotlrn_terms
            where term_id = :term_id