Forum OpenACS Development: get category values within the form

Collapse
Posted by Iuri Sampaio on
Hi there,

I want to set options in a select widget element of an ad_form accordingly with the parent category id passed.

Once the user choose the parent category the form gets refreshed and the subcategory select element get its options filled with the respective values of the children category of the parent category

Here it is the whole chunk of code that adds the parent category and subcategory elements in the ad_form

foreach {category_id category_name} [documents::get_categories -package_id $package_id] {

if {[string equal $category_name "Tipo"]} {
ad_form -extend -name document_ae -form [list \
[list "cat_${category_id}:integer(select)" \
[list label "${category_name}"] \
[list options [documents::category_get_options -parent_id $category_id]] \
[list value ""] \
[list html "onChange \"document.document_ae.__refreshing_p.value='1';document.document_ae.submit();\""]
]]

set child_id [set cat_${category_id}]


ad_form -extend -name document_ae -form [list \
[list "cat_${category_id}:integer(select)" \
[list label "[category::get_name -category_id $category_id]"] \
[list options [documents::category_get_options -parent_id $child_id] \
[list value ""] \
]]

The problem is that the page can not read the value assigned to the parent category variable,
which is the line: set child_id [set cat_${category_id}]

Furthermore i already verified if the parent category value has been passed when the form gets refreshed.

[29/Oct/2010:17:42:24][21191.3061136272][-default:2-] Notice: form:mode - edit
[29/Oct/2010:17:42:24][21191.3061136272][-default:2-] Notice: form:id - document_ae
[29/Oct/2010:17:42:24][21191.3061136272][-default:2-] Notice: __confirmed_p - 0
[29/Oct/2010:17:42:24][21191.3061136272][-default:2-] Notice: __refreshing_p - 1
[29/Oct/2010:17:42:24][21191.3061136272][-default:2-] Notice: __submit_button_name -
[29/Oct/2010:17:42:24][21191.3061136272][-default:2-] Notice: __submit_button_value -
[29/Oct/2010:17:42:24][21191.3061136272][-default:2-] Notice: __key_signature - 23 0 E1F4DE83FEE373DFDF0F67B94FFECF8F47EA3C22
[29/Oct/2010:17:42:24][21191.3061136272][-default:2-] Notice: __key - file_id
[29/Oct/2010:17:42:24][21191.3061136272][-default:2-] Notice: __new_p - 1
[29/Oct/2010:17:42:24][21191.3061136272][-default:2-] Notice: file_id - 1287
[29/Oct/2010:17:42:24][21191.3061136272][-default:2-] Notice: folder_id -
[29/Oct/2010:17:42:24][21191.3061136272][-default:2-] Notice: info2 - MESSAGE KEY MISSING: 'documents.Edit_document'
[29/Oct/2010:17:42:24][21191.3061136272][-default:2-] Notice: upload_file - solisc.gif
[29/Oct/2010:17:42:24][21191.3061136272][-default:2-] Notice: title - solisc
[29/Oct/2010:17:42:24][21191.3061136272][-default:2-] Notice: description - solisc desc
[29/Oct/2010:17:42:24][21191.3061136272][-default:2-] Notice: description.spellcheck - :nospell:
[29/Oct/2010:17:42:24][21191.3061136272][-default:2-] Notice: language - pt_BR
[29/Oct/2010:17:42:24][21191.3061136272][-default:2-] Notice: author - joao
[29/Oct/2010:17:42:24][21191.3061136272][-default:2-] Notice: coauthor - maria
[29/Oct/2010:17:42:24][21191.3061136272][-default:2-] Notice: source - fonte
[29/Oct/2010:17:42:24][21191.3061136272][-default:2-] Notice: group_id - 587
[29/Oct/2010:17:42:24][21191.3061136272][-default:2-] Notice: status - 3
[29/Oct/2010:17:42:24][21191.3061136272][-default:2-] Notice: publish_date.format - YYYY MM DD
[29/Oct/2010:17:42:24][21191.3061136272][-default:2-] Notice: publish_date.year - 2010
[29/Oct/2010:17:42:24][21191.3061136272][-default:2-] Notice: publish_date.month - 10
[29/Oct/2010:17:42:24][21191.3061136272][-default:2-] Notice: publish_date.day - 29
[29/Oct/2010:17:42:24][21191.3061136272][-default:2-] Notice: cat_1231 - 1233
[29/Oct/2010:17:42:24][21191.3061136272][-default:2-] Notice: cat_1201 - 1204

As we can see the last two lines speak for themselves. The values are there.
To certify that the variable cat_1201 is valid to the page i even added the lines:
set varname [ns_set key $myform $i]
set varvalue [ns_set value $myform $i]

as we can see at:

set myform [ns_getform]
if {[string equal "" $myform]} {
ns_log Notice "No Form was submited"
} else {
ns_log Notice "FORM"
ns_set print $myform


for {set i 0} {$i < [ns_set size $myform]} {incr i} {
set varname [ns_set key $myform $i]
set varvalue [ns_set value $myform $i]

ns_log Notice " $varname - $varvalue"
}
}

However i still get the error of "no such variable" to cat_1201

Does anyone have any idea what is wrong?

Collapse
Posted by Malte Sussdorff on
Hi Iuri,

the proper process to achieve this is using Javascript. The logic is that you have a second container (the second drop down), which is not filled with any values (it is empty). Maybe you don't even show it (you can hide it).

But to solve your problem, why don't you say and put this at the top of your page?

for {set i 0} {$i < [ns_set size $myform]} {incr i} {
set varname [ns_set key $myform $i]
set $varname [ns_set value $myform $i]
}
}

set child_id [set cat_${category_id}]

Collapse
Posted by Gustaf Neumann on
Be warned that blindly overwriting vars is never a good idea. You will shoot in your own foot, when your form contains e.g. a varname "i" ...
Collapse
Posted by Brian Fenton on
Hi Iuri

by sheer coincidence, I just provided an example of how to do this here https://openacs.org/forums/message-view?message_id=3473990

take a look and comment there if it doesn't work for you.

cheers
Brian

Collapse
Posted by Iuri Sampaio on
Hi Brian,

The problem is that i am using categories and their names are set dinamically as in

foreach {category_id category_name} [documents::get_categories -package_id $package_id] {
if {[string equal $category_name "Tipo"]} {

ad_form -extend -name document_ae -form [list \
[list "cat_${category_id}:integer(select)" \
[list label "${category_name}"] \
[list options [documents::category_get_options -parent_id $category_id]] \
[list value ""] \
[list html "onChange \"document.document_ae.__refreshing_p.value='1';document.document_ae.submit();\""]
]]

set child_id [set cat_${category_id}]

if {[exists_and_not_null child_id]} { ...

As we can see, after the field is selected the form gets refreshed.

i believe the problem ends up at making the following line to work.

set child_id [set cat_${category_id}]

That's where i get the error of "no such variable"

if i was able to assign what is inside the ad_form's variable cat_${category_id} then i could create the rest without problems

p.s. ajax is another way to do this. But i want to accomplish this way first. Then i will take a look at ajaxhelper.