Forum OpenACS Q&A: ad_form select fields howto

Collapse
Posted by Gregor Obernosterer on
I have been using the new ad_form template which is cool but I still have some troubles with it. Ok this is the situation: I have my form working properly inserting everything I want but when I want to update the contents of an existing ducoment then comes the trouble.I managed to get only back into the form only text or textarea fields. I cannot get any select or multiple select field back on the form to be updated. Do you know how to do it? The problem is actually here:

.....

ad_form -name form_name  -form $widgets -edit_request {
    db_1row document {

        select c.title, d.expiry_date from unido_documents d join 
unido_doc_contents c on d.document_id = c.document_id where 
d.document_id = :document_id

    }
    ad_set_form_values title expiry_date
} -on_submit { ........

I used the standard date widget and I get the complete expiry date on the YY field back, but the other fields for Month and Day are empty.

Thanks in advance

Collapse
Posted by Lars Pind on
Try saying "to_char(d.expiry_date, 'YYYY MM DD HH24 MI')" instead.
Collapse
Posted by Don Baccus on
He'll still have to do a "acquire" operation on it ... *if* he's using the form templater's built-in "date" type and widget (as opposed to a plain text field).

Something like this:

ad_form -name form_name -form {
   expiry_date:date,from_sql(linear_date)
} -select_query {
   select to_char(expiry_date, 'YYYY MM DD HH24 MI') ...
}
will convert the string format (called a "linear date" in format templater lingo, remember that I just wrote the ad_form wrapper) and assign it to expiry_date automatically.

The need to specify "from_sql" is a kludge to get around the fact that the form templater's data types don't give any way to determine if such conversion's necessary or not. I intend to add something like this in the future so conversion between the internal form and external forms can happen by default.

If you're just using a text field to store your date string rather than the fancy built-in date datatype/widget Lars's example works just fine.

Collapse
Posted by Stan Kaufman on
What is the syntax for checking in the "-validate" block the date a user sends back from a date widget? I can't figure this out from ad_form's docs or from the source code.

For instance, the following creates a form and initializes the date widget to today:

set now [db_string now_str "select to_char(current_timestamp,'YYYY-MM-DD') from dual"]

ad_form -name test_form -form {
  my_table:key
  {name:text(text) {label "Name"} {html {size 50}}}
  {start_date:date {label "Start Date"} {value $now}}
} -validate {
  {start_date {start_date:date} }
}
...etc...

But while this successfully flags the date field with an appropriate message ("The day must be between 1 and 30 for the month of September") when you set the widget to "31 September 2002", it dies ugly when a correct date is passed in:

Request Error

       syntax error in expression "start_date:date"
           while executing
       "expr start_date:date"
           ("uplevel" body line 1)
           invoked from within
       "uplevel #$level [list expr $validate_expr"
           (procedure "ad_form" line 455)
...etc...

I was hoping for some good examples in other packages, but none of them (other than survey and acs-person) seem to use ad_form yet, and those don't use dates. News does use dates of course but not ad_form. Furthermore, it interestingly only checks for a valid interval and happily allows a user to enter a bogus "publish date" -- and then barfs ugly with a Request Error (not a more graceful ad_return_error) when [clock scan] can't parse the date.

So, is there a simple syntax that I'm missing here, or does the issue involve the "to_sql", "from_sql" and "to_html" stuff that I haven't yet figured out either? Many TIA!

Collapse
Posted by Stan Kaufman on
Well, I've found that this works. No clue whether this is the Right Way To Do It, but maybe any way that works is the right way:

...
} -validate {
  {start_date {[template::util::date::validate $start_date ""]} "" }
}
...

If there's a better way, I hope someone posts it!

Collapse
Posted by Don Baccus on
Try leaving out the validation block altogether - the built-in validation routine should be called automatically.  The validation block is meant for *additional* validation (i.e. insisting that an end date is greater than a start date, that sort of thing).

I'll try to improve the documentation on this a bit.  The inline documentation tends to assume you know how the form builder works, but as more and more people start using ad_form without having extensively used the templating system's form builder in the past this assumption's not going to hold.

In this case, it is the templating system's form builder, not ad_form, which calls the data type's validation function automatically.  If you haven't used the form builder before, there's no way you'd know that from just reading ad_form's docs ...

Collapse
Posted by Stan Kaufman on
Zounds! It does it automagically! Amazing! Don, thanks for clarifying this and for writing such a useful proc to begin with! ad_form is way cool!
Collapse
Posted by Roberto Mello on
We are looking for someone to write a short document to be included in the templating docs about how to use ad_form. This would be extremely useful for newcomers to the toolkit. The document should assume no previous knowledge of the templating system form builder.

Who would like to write it?

Collapse
Posted by Jon Griffin on
I already started one when I ported acs-person to use ad_form.

Collapse
Posted by Stan Kaufman on
I'd be happy to help with that, Jon -- to add/elaborate from the perspective of a happy 3.2.5 camper cringing my way into the Brave New World of 4.x...
Collapse
Posted by Dave Bauer on
I have some ad_form doc I started also at: http://www.thedesignexperience.org/using_ad_form.html
Collapse
Posted by Jon Griffin on
I can't seem to find my notes so I will start from scratch again tomorrow.
Collapse
Posted by Jon Griffin on
I updated my ad_form doc at: http://jongriffin.com/static/openacs/ad_form/using-ad-form

Changes include:

  • Fixed syntax for select and checkboxes
  • Fixed to_timestamp syntax
Collapse
Posted by Robert Locke on
Can we integrate Jon's wonderful ad_form documentation into the OACS distribution itself?
Collapse
Posted by Tom Jackson on

Is it possible that ad_form is used somewhere so I can load a module and see how it actually works. It must be a neat thing that I should be using, but I don't think there are enough docs out there to explain it to me.

Although Jon's doc talks about the tcl page use of ad_form, what about the adp use? What about files other than these which affect the display?

Did I hear Don right that this is basically the template system form builder wrapped up in a nicer package?

If someone directs me to complete examples that work, I promise to use ad_form in my future projects! :)

Collapse
Posted by Dan Wickstrom on
The survey packages make extensive use of ad_form, so it would be your best bet for working examples.

And yes, you did hear correctly, ad_form is a nice wrapper around the template system form builder.  If necessary, you can intermix the template system form-builder commands with ad_form.

Collapse
Posted by Jon Griffin on
You are more than welcome to put the docs in the distro.

I will add the .adp and query files to it later in the week. It might be better for now to just link to it and I will make it sgml when I return home later in the week.