Forum OpenACS Q&A: Question on ad_form and select options

Could you please help me with this minor problem?

I have read the documentation of ad_form but I believe I am stuck 😟( Basically, I am trying to find out how ad_form works and have build a small silly app from the documentation code examples that uploads recipes into a database table. Table contains Title, Description and Category of the Recipe.

I want to use the ad_form to enter Title,Description and
Category(by selecting the Category select box on the form)

The problem is that the form complains that Category is required even after I've selected a value from
the select box.

For the sake of simplicity, title, description and category_id are all text datatype. Category_id gets values from another table and I use db_list_of_lists to bring them in.

I read from the docs that I can build the select box using the option "-on_request" and that works fine.
Now do I have to set the form value category_id using: the option "-in_submit"? I used "ad_set_element_value..." to no avail. But...Title and description have no problem getting set.

Ok, this is a silly example but do I do wrong ?

Thanks a million in advance!

The code for the form is:

ad_page_contract {
Simple add/edit form
This is a simple form to add recipe information into our table
} {
my_table_key:optional
}

set foo_options [db_list_of_lists foo_option_list "select category_id from categories" ]

ad_form -name add_recipe -form {

my_table_key:key(recipes_id_seq)

{title:text,nospell {label "Title"}
{html {size 40}}}
{description:text(textarea) {label "Enter the description of your recipe"} {html {rows 3 cols 50} } }
} -validate {
{description
{[string length description] >= 3}
"\"description\" must be a string containing three or more characters"
}
} -on_request {{category_id:text(select) {label "Which Category"} {options $foo_options}}
} -on_submit {
ad_set_element_value -element category_id $category_id
} -new_data {
db_dml do_insert "insert into recipes (title,description,category_id) values (:title,:description,:category_id)"
} -after_submit {
# ad_returnredirect "somewhere"
ad_script_abort
}

Collapse
Posted by Claudio Pasolini on
Hi Nick,

your code example contains two errors.

You can use the -on_request block to populate the form's elements, but you should put the definition of all the elements (in your example category_id is not defined ) under -form.

To populate a selection box you have to provide two values, the first being a description and the second being the corresponding id. So you have to supply category_description and category_id to set up your foo_options.

Actually there is no need, for the sake of this example, of a -request_block: simply put this element definition under -form and you are done:

{category_id:text(select)
{label "Which Category"}
{options [db_list_of_lists foo_option_list "select category_description, category_id from categories" ]}
}

A word of caution. If you were using the categories table of the categories package you should join the categories table with category_translations to get the category description.

Collapse
Posted by Nick Kokkos on
Thank you Claudio!
I am just learning the ropes here and was confused. Also, the categories table I use has nothing to do with the categories package. It's just a simple table with no id. That's where my problem was. Thanks again!