Dave showed me a neat trick for creating multiple items on a single ad_form page, using arrays. I can get everything to work except for the dates, for some reason. I bet the solution to this is pretty simple, but I've been working on it for more than an hour, and I seem to be having no luck.
The idea here is to create multiple Tasks in one ad_form page.
If I leave the end_date out, it works fine, but when I try to reference end_date($i) inside the ns_log statement, it returns this error:
can't read "end_date(1)": no such element in array
while executing
"ns_log Notice "end_date: $end_date($i)""
invoked from within
"if {[info exists start_date]} {
for {set i 1} {$i <= $number} {incr i} {
ns_log Notice "end_date: $end_date($i)"
}
}"
Is there any reason dates are different than other items?
I've tried a lot of things, but none of them worked.
Here is the simplified .adp page:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<table>
<formtemplate id="add_edit" style="standard-lars">
<multiple name="num">
<tr>
<td>@num.rownum@</td>
<td><formwidget id="mailto:task_title.@num.rownum@">
<td><formwidget id="mailto:end_date.@num.rownum@"></td>
</multiple>
<tr><td colspan="99" align="center"><input type="submit" name="formbutton:ok" value = " OK "></td>
</formtemplate>
</table>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Here is the simplified .tcl page:
ad_page_contract {
Add/edit form for tasks
@author mailto:jader@bread.com
@creation-date 2003-07-28
@cvs-id $Id: task-add-edit.tcl,v 1.4 2003/08/01 20:51:04 jader Exp $
@return num num is used as a multirow datasource to iterate over the ad_form elements
@param number The number of Tasks to create
@param task_id Specifies the item for the task (every revision of a task shares the same task_id)
} {
{number:integer "1"}
task_id:multiple,integer,optional
task_title:array,optional
end_date:array,optional
} -properties {
num:multirow
}
template::multirow create num number
for {set i 1} {$i <= $number} {incr i} {
template::multirow append num $i
}
if {[info exists start_date]} {
for {set i 1} {$i <= $number} {incr i} {
ns_log Notice "end_date: $end_date($i)"
}
}
# the unique identifier for this package
set package_id [ad_conn package_id]
set user_id [ad_maybe_redirect_for_registration]
ad_form -name add_edit -form {
task_id:key
{number:text(hidden)
{value $number}
}
} -new_data {
for {set i 1} {$i <= $number} {incr i} {
# ns_log Notice "task_title($i) = $task_title($i)"
db_exec_plsql new_task_item { *SQL* }
}
} -select_query_name task_query -after_submit {
ad_returnredirect "one?item_id=$project_item_id&project_id=$project_id"
ad_script_abort
}
for {set i 0} {$i <= $number} {incr i} {
# reading this code, you may wonder why we put the .$i at the end.
# DaveB showed me this trick. It lets you make a multiple out of
# the items by stuffing them in an array. Long live DaveB.
append add_edit_definition "
{task_title.$i:text
{label \"Subject \#$i\"}
{html {size 30}}
}
{end_date.$i:date
{label \"End date\"}
{format \"MONTH DD YYYY\"}
{help}
}
"
}
ad_form -extend -name add_edit -form $add_edit_definition -select_query_name task_query