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 -r1.3 -r1.4 --- openacs-4/packages/xowiki/tcl/form-field-procs.tcl 26 Jun 2007 10:26:48 -0000 1.3 +++ openacs-4/packages/xowiki/tcl/form-field-procs.tcl 28 Jun 2007 13:02:51 -0000 1.4 @@ -22,13 +22,35 @@ {value ""} {spec ""} {help_text ""} + {error_msg ""} + {validator ""} } FormField instproc init {} { if {![my exists label]} {my label [string totitle [my name]]} if {[my exists id]} {my html(id) [my id]} #my msg "calling config_from_spec '[my spec]'" my config_from_spec [my spec] } + + FormField instproc validate {value obj} { + my instvar name required + if {$required && $value eq ""} { + my instvar label + return [_ acs-templating.Element_is_required] + } + # todo value type checker (through subtypes, check only if necessary) + if {[my validator] ne ""} { + set r [$obj [my validator] $value] + #my msg "validator [my validator] /[$obj procsearch [my validator]]/ returned $r" + if {$r != 1} { + set cl [namespace tail [lindex [$obj procsearch [my validator]] 0]] + my msg xowiki.$cl-[my validator] + return [_ xowiki.$cl-[my validator]] + } + } + return "" + } + FormField instproc config_from_spec {spec} { my instvar type options widget_type if {[my info class] eq [self class]} { @@ -50,6 +72,7 @@ numeric {my class [self class]::text; #for the time being } select {my class [self class]::select} + scale {my class [self class]::scale} month {my class [self class]::month} date {my class [self class]::date} label=* {my label [lindex [split $s =] 1]} @@ -64,7 +87,7 @@ } FormField instproc asWidgetSpec {} { - my instvar widget_type options label help_text format html + my instvar widget_type options label help_text format html display_html set spec $widget_type if {[my exists spell]} {append spec ",[expr {[my spell] ? {} : {no}}]spell"} @@ -85,7 +108,9 @@ if {[my exists format]} { append spec " {format " [list $format] "} " } - + #if {[my exists display_html]} { + # append spec " {display_value " [list [my set display_html]] "} " + #} if {$help_text ne ""} { if {[string match "#*#" $help_text]} { set internationalized [_ [string trim $help_text #]] @@ -119,6 +144,12 @@ } } my render_form_widget + if {[my error_msg] ne ""} { + ::html::div -class form-error { + my instvar label + ::html::t -disableOutputEscaping [my error_msg] + } + } } } FormField instproc renderValue {v} { @@ -201,6 +232,13 @@ } } + Class FormField::radio -superclass FormField -parameter { + {options ""} + } + FormField::radio instproc initialize {} { + my set widget_type text(radio) + } + Class FormField::select -superclass FormField -parameter { {options ""} } @@ -217,13 +255,22 @@ next } - Class FormField::boolean -superclass FormField -superclass FormField::select + Class FormField::boolean -superclass FormField -superclass FormField::radio FormField::boolean instproc initialize {} { my options {{No f} {Yes t}} next } - + #Class FormField::scale -superclass FormField -parameter {{n 5}} + #FormField::scale instproc initialize {} { + # my instvar n display_html + # my set widget_type text + # for {set i 1} {$i < $n} {incr i} { + # set checked "" + # if {[my exists value] && [my value] == $i} {set checked " checked='checked'"} + # append display_html " " + # } + #} # # a few test cases 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 -r1.124 -r1.125 --- openacs-4/packages/xowiki/tcl/xowiki-procs.tcl 27 Jun 2007 09:27:49 -0000 1.124 +++ openacs-4/packages/xowiki/tcl/xowiki-procs.tcl 28 Jun 2007 13:02:51 -0000 1.125 @@ -45,14 +45,19 @@ ::xowiki::Page::slot::name set required true ::xowiki::Page::slot::name set help_text #xowiki.Page-name-help_text# ::xowiki::Page::slot::name set datatype text + ::xowiki::Page::slot::name set validator validate_name + ::xowiki::Page::slot::title set pretty_name #xowiki.Page-title# ::xowiki::Page::slot::title set required true ::xowiki::Page::slot::title set datatype text + ::xowiki::Page::slot::description set pretty_name #xowiki.Page-description# ::xowiki::Page::slot::description set spec "textarea,cols=80,rows=2" ::xowiki::Page::slot::description set datatype text + ::xowiki::Page::slot::text set pretty_name #xowiki.Page-text# ::xowiki::Page::slot::text set datatype text + ::xowiki::Page::slot::nls_language set pretty_name #xowiki.Page-nls_language# ::xowiki::Page::slot::nls_language set datatype text ::xowiki::Page::slot::nls_language set spec {select,options=[xowiki::locales]} @@ -757,6 +762,12 @@ return $field_name:$spec } + Page instproc validate_name {name} { + upvar nls_language nls_language + my set data [self] ;# for the time being; change clobbering when validate_name becomes a method + return [::xowiki::validate_name] + } + Page instproc update_references {page_id references} { db_dml [my qn delete_references] \ "delete from xowiki_references where page = $page_id" @@ -1176,6 +1187,7 @@ FormInstance ad_instproc set_form_data {} { Store the instance attributes in the form. } { + #my msg "set_form_value instance attributes = [my instance_attributes]" foreach {att value} [my instance_attributes] { #my msg "set_form_value $att $value" my set_form_value $att $value @@ -1186,22 +1198,32 @@ Get the values from the form and store it as instance attributes. } { + set form_errors [list] set form [lindex [my get_from_template form] 0] if {$form ne ""} { + array set name_map {"__name" name "__title" title "__page_order" page_order} array set __ia [my set instance_attributes] # we have a form, we get for the time being all variables foreach att [::xo::cc array names form_parameter] { + set matt [expr {[info exists name_map($att)] ? $name_map($att) : $att}] + set f [my create_form_field -name $att -slot [my find_slot $matt]] + set value [::xo::cc form_parameter $att] + set form_error [$f validate $value [self]] + if {$form_error ne ""} { + lappend form_errors $att $form_error + } switch -- $att { __object_name {} - __name {my set name [::xo::cc form_parameter $att]} - __title {my set title [::xo::cc form_parameter $att]} - __page_order {my set page_order [::xo::cc form_parameter $att]} - default {set __ia($att) [::xo::cc form_parameter $att]} + __name {my set $matt $value} + __title {my set $matt $value} + __page_order {my set $matt $value} + default {set __ia($att) $value} } } my log "--set instance attributes to [array get __ia]" my set instance_attributes [array get __ia] } + return $form_errors } FormInstance instproc form_attributes {} { @@ -1240,6 +1262,7 @@ return [next] } else { set form [lindex [my get_from_template form] 0] + #my msg "we have a form" dom parse -simple -html $form doc $doc documentElement root my set_form_data @@ -1276,19 +1299,17 @@ Method to be called from a submit button of the form } { my instvar package_id name - my get_form_data - my set data [self] ;# for the time being; change clobbering when validate_name becomes a method - set ok [::xowiki::validate_name] - my log "--forminstance name='$name', old_name=[::xo::cc form_parameter __object_name] ok=$ok" - if {$ok} { + set validation_errors [my get_form_data] + if {$validation_errors ne [list]} { + foreach {att msg} $validation_errors { + my msg "Error in $name: $att" + } + my edit -validation_errors $validation_errors + } else { my save_data [::xo::cc form_parameter __object_name ""] my log "--forminstance redirect to [$package_id pretty_link $name]" $package_id returnredirect \ [my query_parameter "return_url" [$package_id pretty_link $name]] - } else { - util_user_message -message "Another item with the name name $name exists already" - set name [::xo::cc form_parameter __object_name] - $package_id returnredirect [::xo::cc url]?m=edit } } Index: openacs-4/packages/xowiki/tcl/xowiki-www-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/xowiki/tcl/xowiki-www-procs.tcl,v diff -u -r1.56 -r1.57 --- openacs-4/packages/xowiki/tcl/xowiki-www-procs.tcl 27 Jun 2007 09:27:49 -0000 1.56 +++ openacs-4/packages/xowiki/tcl/xowiki-www-procs.tcl 28 Jun 2007 13:02:51 -0000 1.57 @@ -210,7 +210,11 @@ } } - Page instproc edit {{-new:boolean false} {-autoname:boolean false}} { + Page instproc edit { + {-new:boolean false} + {-autoname:boolean false} + {-validaton_errors ""} + } { my instvar package_id item_id revision_id $package_id instvar folder_id ;# this is the root folder @@ -332,15 +336,21 @@ -label $label \ -type [expr {[$slot exists datatype] ? [$slot set datatype] : "text"}] \ -help_text [expr {[$slot exists help_text] ? [$slot set help_text] : ""}] \ + -validator [expr {[$slot exists validator] ? [$slot set validator] : ""}] \ -required [expr {[$slot exists required] ? [$slot set required] : "false"}] \ -spec [join $spec_list ,] \ ] $f destroy_on_cleanup - $f configure $configuration + eval $f configure $configuration return $f } - PageInstance instproc create_form_field {-name -slot {-spec ""} {-configuration ""}} { + PageInstance instproc create_form_field { + -name + -slot + {-spec ""} + {-configuration ""} + } { set short_spec [my get_short_spec $name] set spec_list [list] if {$short_spec ne ""} {lappend spec_list $short_spec} @@ -350,15 +360,39 @@ return $f } - FormInstance instproc edit {} { + FormInstance instproc create_form_fields {fields root fcn validation_errors} { + array set errors $validation_errors + foreach {formatt att} $fields { + set error_msg "" + if {[info exists errors($formatt)]} {set error_msg $errors($formatt)} + + set f [my create_form_field -name $formatt -slot [my find_slot $att] \ + -configuration [list -value [my set $att] -error_msg $error_msg]] + $root insertBeforeFromScript {$f render_item} $fcn + } + } + + FormInstance instproc edit { + {-validation_errors ""} + } { my instvar page_template doc root package_id - + + if {[my form_parameter __form_action ""] eq "save-form-data"} { + set validation_errors [my get_form_data] + if {$validation_errors ne [list]} { + foreach {att msg} $validation_errors { + my msg "Error in $att: $msg" + } + # reset the name in error cases to the original one + my set name [my form_parameter __object_name] + } else { + #$package_id returnredirect [::xo::cc url]?m=edit + #my edit -validation_errors $validation_errors + } + } set form [lindex [my get_from_template form] 0] - my log "--forminstance form='$form'" - set anon_instances [my get_from_template anon_instances] - #set form_constraints [my get_from_template form_constraints] - #my log "--forminstance anon_instances='$anon_instances' fc='$form_constraints'" - my log "--forminstance anon_instances='$anon_instances'" + set anon_instances [my get_from_template anon_instances] + if {$form eq ""} { #next -autoname $anon_instances -form_constraints $form_constraints next -autoname $anon_instances @@ -370,22 +404,16 @@ $root firstChild fcn $root insertBeforeFromScript { ::html::input -type hidden -name __object_name -value [my name] + ::html::input -type hidden -name __form_action -value save-form-data } $fcn if {$anon_instances eq "f"} { - set f [my create_form_field -name __name -slot [my find_slot name] \ - -configuration [list -value [my set name]]] - $root insertBeforeFromScript {$f render_item} $fcn - if {[$package_id show_page_order]} { - set f [my create_form_field -name __page_order -slot [my find_slot page_order] \ - -configuration [list -value [my set page_order]]] - $root insertBeforeFromScript {$f render_item} $fcn - } - - set f [my create_form_field -name __title -slot [my find_slot title] \ - -configuration [list -value [my set title]]] - $root insertBeforeFromScript {$f render_item} $fcn + set fields {__name name __page_order page_order __title title} + } else { + set fields {__name name __title title} + } + my create_form_fields $fields $root $fcn $validation_errors } $root appendFromScript { @@ -396,7 +424,7 @@ if {$form eq ""} { my msg "no form found in page [$page_template name]" } else { - $form setAttribute action [$package_id pretty_link [my name]]?m=save-form-data method POST + $form setAttribute action [$package_id pretty_link [my name]]?m=edit method POST set oldCSSClass [expr {[$form hasAttribute class] ? [$form getAttribute class] : ""}] $form setAttribute class [string trim "$oldCSSClass margin-form"] }