Class ::xowiki::formfield::numeric (public)
::xotcl::Class ::xowiki::formfield::numeric [ -format format ] \ [ -connection_locale connection_locale ] [ -strict strict ] \ [ -keep_string_rep keep_string_rep ]
Defined in /var/www/openacs.org/packages/xowiki/tcl/form-field-procs.tcl
Field with numeric content. Depending on the format, the accepted value can be either an integer or a floating point number. The widget performs localization based on the setting of :locale. In case 'keep_string_rep' is not true, the widget converts the value to an internal representation to be able to evaluate numeric expressions by this. When 'keep_string_rep' is true, the original string representation is kept, and only validation is performed.
- Switches:
- -format (optional)
- format for output and determining integer property
- -connection_locale (optional)
- when set, use the connection locale as source for internationalized input
- -strict (optional)
- when set, use just use the locale for input checking. Otherwise, always accept as fallback what is accepted by en_US.
- -keep_string_rep (optional)
- when true, do not convert from and to the internal representation, but preserve the original string.
- Testcases:
- No testcase defined.
Source code: namespace eval ::xowiki::formfield {} ::nsf::object::alloc ::xotcl::Class ::xowiki::formfield::numeric {set :__default_metaclass ::xotcl::Class set :__default_superclass ::xotcl::Object} ::xowiki::formfield::numeric instproc pretty_value value { return [:convert_to_external $value] } ::xowiki::formfield::numeric instproc check=numeric value { #ns_log notice "=== numeric: value '$value' locale '[:locale]' is_integer '${:is_integer}'" return [expr {[catch {:convert_to_internal_value $value}] == 0}] } ::xowiki::formfield::numeric instproc convert_to_internal {} { if {!${:keep_string_rep} && ${:value} ne ""} { # # The value has been already checked against the validator, so # the conversion should be smooth. # set :value [:convert_to_internal_value ${:value}] ${:object} set_property -new 1 ${:name} ${:value} } } ::xowiki::formfield::numeric instproc render_input {} { # # Prevent inserting invalid value; we allow currently just a # single dot, comma for floats or "-" to be entered. Maybe some # of this functionality should be blocked conditionally. Paste # filtering is also currently just half-hearted but probably # sufficient. # set punctuation [expr {[string match %*f ${:format}] ? ",." : "" }] template::add_script -section body -script [subst -nocommands [ns_trim { var e = document.getElementById('${:id}'); e.addEventListener('keypress', function (e) { if (!(/[0-9]|[.,]|-/.test(e.key))){ e.preventDefault(); } else if (/[$punctuation-]/.test(e.key) && e.target.value.includes(e.key)) { e.preventDefault(); } }); e.addEventListener('paste', function (e) { var pasted = e.clipboardData || window.clipboardData; var text = pasted.getData('Text'); if (!text.match(/[0-9$punctuation]|-/)) { e.preventDefault(); } }); }]] next } ::xowiki::formfield::numeric instproc answer_check=eq {} { try { set x [:convert_to_internal_value ${:value}] set y [:convert_to_internal_value [lindex ${:correct_when} 1]] #ns_log notice "numeric answer_check=eq " "'[lindex ${:correct_when} 1]' with '${:value}'" "->" "expr {$x == $y}" set result [expr {$x == $y}] } on error {errorMsg} { ns_log warning "numeric answer_check=eq received exception while comparing " "'[lindex ${:correct_when} 1]' with '${:value}'" $errorMsg set result 0 } return $result } ::xowiki::formfield::numeric instproc initialize {} { next set :widget_type numeric # check, if we have an integer format set :is_integer [regexp {%[0-9.]*d} ${:format}] if {${:connection_locale} && [ns_conn isconnected]} { set :locale [ad_conn locale] } } ::xowiki::formfield::numeric instproc convert_to_internal_value value { #ns_log notice "convert_to_internal_value called with value '$value'" try { lc_parse_number $value ${:locale} ${:is_integer} } on ok {result} { } on error {errorMsg} { #ns_log notice "numeric instproc convert_to_internal <$value> ${:locale} -> $errorMsg ($::errorCode)" if {${:strict} == 0 && ${:locale} ne "en_US"} { try { lc_parse_number $value en_US ${:is_integer} } on ok {result} { } } else { throw $::errorInfo $errorMsg } } #ns_log notice "convert_to_internal_value called with value '$value' -> $result" return $result } ::xowiki::formfield::numeric instproc convert_to_external value { #ns_log notice "convert_to_external ${:name} keep_string_rep ${:keep_string_rep}" if {${:keep_string_rep}} { return $value } if {$value eq ""} { set result "" } else { ad_try { set value [lc_numeric $value ${:format} ${:locale}] } on error {errorMsg} { util_user_message -message "${:label}: $errorMsg (locale=${:locale})" } # # Try to parse finally the value against the format # set converted_value $value ad_try { scan $value ${:format} result } on error {errMsg} { set result $value } } #ns_log notice "convert_to_external ${:name} keep_string_rep ${:keep_string_rep} -> $result" return $result } ::xowiki::formfield::numeric instparametercmd format ::xowiki::formfield::numeric instparametercmd keep_string_rep ::xowiki::formfield::numeric instparametercmd validator ::xowiki::formfield::numeric instparametercmd strict ::xowiki::formfield::numeric instparametercmd connection_locale ::nsf::relation::set ::xowiki::formfield::numeric superclass ::xowiki::formfield::text ::nx::slotObj -container slot ::xowiki::formfield::numeric ::xowiki::formfield::numeric::slot eval {set :__parameter { {format %.2f} {connection_locale 1} {strict 0} {keep_string_rep 0} }} ::nsf::object::alloc ::xotcl::Attribute ::xowiki::formfield::numeric::slot::strict {set :accessor public set :configurable true set :convert false set :default 0 set :defaultmethods {} set :disposition alias set :domain ::xowiki::formfield::numeric set :incremental 0 set :manager ::xowiki::formfield::numeric::slot::strict set :methodname strict set :multiplicity 1..1 set :name strict set :per-object false set :position 0 set :required false set :substdefault 0b111 set :trace none : init} ::nsf::object::alloc ::xotcl::Attribute ::xowiki::formfield::numeric::slot::keep_string_rep {set :accessor public set :configurable true set :convert false set :default 0 set :defaultmethods {} set :disposition alias set :domain ::xowiki::formfield::numeric set :incremental 0 set :manager ::xowiki::formfield::numeric::slot::keep_string_rep set :methodname keep_string_rep set :multiplicity 1..1 set :name keep_string_rep set :per-object false set :position 0 set :required false set :substdefault 0b111 set :trace none : init} ::nsf::object::alloc ::xotcl::Attribute ::xowiki::formfield::numeric::slot::format {set :accessor public set :configurable true set :convert false set :default %.2f set :defaultmethods {} set :disposition alias set :domain ::xowiki::formfield::numeric set :incremental 0 set :manager ::xowiki::formfield::numeric::slot::format set :methodname format set :multiplicity 1..1 set :name format set :per-object false set :position 0 set :required false set :substdefault 0b111 set :trace none : init} ::nsf::object::alloc ::xotcl::Attribute ::xowiki::formfield::numeric::slot::validator {set :accessor public set :configurable true set :convert false set :default numeric set :defaultmethods {} set :disposition alias set :domain ::xowiki::formfield::numeric set :incremental false set :manager ::xowiki::formfield::numeric::slot::validator set :methodname validator set :multiplicity 1..1 set :name validator set :per-object false set :position 0 set :required false set :substdefault 0b111 set :trace none : init} ::nsf::object::alloc ::xotcl::Attribute ::xowiki::formfield::numeric::slot::connection_locale {set :accessor public set :configurable true set :convert false set :default 1 set :defaultmethods {} set :disposition alias set :domain ::xowiki::formfield::numeric set :incremental 0 set :manager ::xowiki::formfield::numeric::slot::connection_locale set :methodname connection_locale set :multiplicity 1..1 set :name connection_locale set :per-object false set :position 0 set :required false set :substdefault 0b111 set :trace none : init}XQL Not present: Generic, PostgreSQL, Oracle