Forum OpenACS Development: ad_form again

Collapse
Posted by Ola Hansson on
Hi, I have a new ad_form question. I love ad_form, but it doesn't do as I want. 😊 The following bombs, so how can I dynamically choose the type of widget, as in this example, or the datatype, etc?

The documentation for ad_form says this, which I don't understand very well:

In order to make it possible to use ad_form to build common form snippets within procs, code blocks are executed at the current template parse level. This is necessary if validate and similar blocks are to have access to the form's contents but may cause surprises for the unwary. So be wary. On the other hand when subst is called, for instance when setting values in the form, the caller's level is used. Why do this? A proc building a common form snippet may need to build a list of valid select elements or similarly compute values that need to be set in the form, and these should be computed locally.
if $some_condition_p {
    set widget text
} else {
    set widget inform
}

ad_form -form {
    test_id:key

    {test:text($widget)                   {label "Test"}
	{value {This is a test}}}
}

....
Does anyone know ad_form well yet, except Don?

Thanks,

Collapse
2: Response to ad_form again (response to 1)
Posted by Dave Bauer on
Ola,

That doesn't work.

Try this:

if {$some_condition_p} {
     ad_form -extend -name formname -form {
            {test:text(text) {value {This is a test}} }
     }
} else {
     ad_form -extend -name formname -form {
          {test:text(inform) {value {This is a test}} }
     }
}

or some variation on that theme. The idea is to build up the form using -extend. You can't pass in a variable as the value of -form {}.
Collapse
Posted by Jade Rubick on
There is a workaround that lets you substitute a variable into the ad_form declaration. Use [set variablename].
Collapse
3: Response to ad_form again (response to 1)
Posted by Jeff Davis on
If you want variable interpolation you need to build up the
list with list calls like:

-form [list my_table_key:key(my_table_sequence) [list "test:test($widget)" {label "Test"} ... ] ]

Only caveat is that I have not really used ad_form and the function is too large to actually understand :)

Collapse
4: Response to ad_form again (response to 1)
Posted by Ola Hansson on
Ah! Both answers are very good. I could never get -extend to work before but now I do (I didn't realize that you should call ad_form several times, without -extend in the first call, to extend a form).

Jeff's code works!! 😊 What beats me is why you must call [list] for the outermost two lists and can't just use {} instead. ({} does not work, I tried).

Isn't {} = [list] ?

Collapse
5: Response to ad_form again (response to 1)
Posted by Jeff Davis on
{} is list but without any variable interpolation. Chapter 10 of Brent Welch's book is a good place to read about this. see http://www.beedub.com/book/3rd/Eval.pdf