Index: openacs-4/packages/xowiki/tcl/form-field-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/xowiki/tcl/form-field-procs.tcl,v diff -u -N -r1.248.2.34 -r1.248.2.35 --- openacs-4/packages/xowiki/tcl/form-field-procs.tcl 29 Oct 2016 13:25:07 -0000 1.248.2.34 +++ openacs-4/packages/xowiki/tcl/form-field-procs.tcl 23 Nov 2016 13:45:53 -0000 1.248.2.35 @@ -3356,6 +3356,22 @@ } } + CompoundField instproc add_component {entry} { + # + # Add a single component dynamically to the list of already + # existing components and return the component as result. + # + my lappend structure $entry + lassign $entry name spec + set c [::xowiki::formfield::FormField create [self]::$name \ + -name [my name].$name -id [my id].$name \ + -locale [my locale] -object [my object] \ + -spec $spec] + my set component_index([my name].$name) $c + my lappend components $c + return $c + } + CompoundField instproc get_component {component_name} { set key component_index([my name].$component_name) if {[my exists $key]} { Index: openacs-4/packages/xowiki/tcl/repeat-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/xowiki/tcl/repeat-procs.tcl,v diff -u -N -r1.5.2.1 -r1.5.2.2 --- openacs-4/packages/xowiki/tcl/repeat-procs.tcl 3 Oct 2016 13:35:01 -0000 1.5.2.1 +++ openacs-4/packages/xowiki/tcl/repeat-procs.tcl 23 Nov 2016 13:45:53 -0000 1.5.2.2 @@ -78,13 +78,13 @@ if {[my exists __initialized_repeat]} {return} next + my set __initialized_repeat 1 # # Derive the spec of the contained items from the spec of the # container. # - set itemSpec [lindex [my item_spec] 1] - set is_required [lindex [my item_spec] 0] + lassign [my item_spec] isRequired itemSpec # # Use item .0 as template for other items in .js (e.g. blank an @@ -98,12 +98,10 @@ # # Add max content items (1 .. max) and build form fields # - for {set i 1} {$i <= [my max]} {incr i} { - if {$i <= [my min] && $is_required} { - lappend components [list $i $itemSpec,required,label=$i] - } else { - lappend components [list $i $itemSpec,label=$i] - } + set max [my max] + #set max 1 ;# dynamic repeat fields: one might set max to 1 to force repeat fields to be created dynamically (missing js support) + for {set i 1} {$i <= $max} {incr i} { + lappend components [my component_item_spec $i $itemSpec $isRequired] } my create_components $components @@ -117,6 +115,47 @@ } } + repeatContainer instproc component_item_spec {i itemSpec isRequired} { + # + # Return a single itemspec suited for the nth component, derived + # from the repeatable formfield spec. + # + if {$i <= [my min] && $isRequired} { + set componentItemSpec [list $i $itemSpec,required,label=$i] + } else { + set componentItemSpec [list $i $itemSpec,label=$i] + } + return $componentItemSpec + } + + repeatContainer instproc require_component {i} { + # + # Require the nth component of a repeat field + # + lassign [my item_spec] isRequired itemSpec + set componentItemSpec [my component_item_spec $i $itemSpec $isRequired] + ns_log notice "dynamic repeat field: add component on the fly: $componentItemSpec" + my add_component $componentItemSpec + } + + repeatContainer instproc set_compound_value value { + # + # Before setting compound values, check if we have the repeat + # strucure already set. + # + set neededComponents [expr {[llength $value] / 2}] + set availableComponents [llength ${:components}] + #ns_log notice "[self] repeatContainer set_compound_value <$value> have $availableComponents needed $neededComponents" + if {$neededComponents > $availableComponents} { + lassign [my item_spec] isRequired itemSpec + for {set i $availableComponents} {$i < $neededComponents} {incr i} { + :require_component $i + } + } + + next + } + repeatContainer instproc convert_to_internal {} { set values [my value] my trim_values @@ -155,6 +194,7 @@ return $highestCount } + repeatContainer instproc render_input {} { # # Render content of the container within in a fieldset, Index: openacs-4/packages/xowiki/tcl/xowiki-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/xowiki/tcl/xowiki-procs.tcl,v diff -u -N -r1.485.2.31 -r1.485.2.32 --- openacs-4/packages/xowiki/tcl/xowiki-procs.tcl 3 Nov 2016 10:50:56 -0000 1.485.2.31 +++ openacs-4/packages/xowiki/tcl/xowiki-procs.tcl 23 Nov 2016 13:45:54 -0000 1.485.2.32 @@ -2777,11 +2777,29 @@ # foreach name_and_spec [my get_form_constraints] { regexp {^([^:]+):(.*)$} $name_and_spec _ spec_name short_spec + if {[string match $spec_name $name]} { set f [my create_form_fields_from_form_constraints [list $name:$short_spec]] set $key $f return $f } + + # + # Maybe, this was a repeat field, and we have to create the nth + # component dynamically. + # + if {[regexp {^(.*)[.](\d+)$} $name . root number]} { + ns_log notice "dynamic repeat field <$root> number $number [info exists ::_form_field_names($root)]" + if {[info exists ::_form_field_names($root)]} { + set repeatField [set ::_form_field_names($root)] + # + # Maybe we have to check the order (when e.g. text.5 is passed in before text.3) + # + set f [$repeatField require_component $number] + ns_log notice "dynamic repeat field required $f" + return $f + } + } } if {$name ni {langmarks fontname fontsize formatblock}} { set names [list]