Re: Validation and tablelist trouble
- From: Csaba Nemethi <csaba.nemethi@xxxxxxxxxxx>
- Date: Fri, 06 Jan 2006 12:56:24 +0100
mdn schrieb:
Hi,
I don't understand the use of validation with interactive cell editing in tablelists. Trouble arises when entry's validatecommand edits the content of the entry widget. In that case the content is always accepted, even when it is invalid. Has this anything to do with a remark in the manpage of edit: "The validate option will also set itself to none when you edit the entry widget from within either the validateCommand"?
Below you find a testcase. Anything with more than 5 characters should be rejected. If you change "val1" by "value1" than it is, however when you add an extra space, so "value1 ", so that the trimming is activated, the too long value is accepted. How do you solve this?
tcl-8.4.9 and tablelist-4.0.
Regards, Michiel
package require tablelist
proc trimEntry {w} { set val [$w get] set newVal [string trim $val] if {$val != $newVal} { $w delete 0 end $w insert end $newVal } return }
proc checkLength {w} { trimEntry $w set val [$w get] # after idle "$w config -validate focusout" return [expr [string length $val] < 5] }
proc startICE {tablelist row col txt} { [$tablelist editwinpath] configure -validate focusout \ -validatecommand [list checkLength %W] \ -invalidcommand [list invalidcmd $tablelist] return }
proc endICE {tablelist row col txt} { set entryW [$tablelist editwinpath] $entryW validate return [$entryW get] }
proc invalidcmd {tablelist} { bell $tablelist rejectinput return }
::tablelist::tablelist .tl -editstartcommand startICE -editendcommand endICE
.tl insertcolumnlist end {0 Col1 0 Col2}
.tl columnconfigure 0 -editable 1
.tl columnconfigure 1 -editable 1
.tl insert 0 {val1 val2}
pack .tl -fill both -expand yes
What you describe here is not at all Tablelist-specific. Entry validation works the same, whether the entry is embedded into a tablelist widget or not.
There are a few things in your code and in your analysis that are not OK:
1. It is not correct that texts like "value1 " are accepted by the
validation. Your trimEntry proc removes the trailing space characters, and what is eventually inserted into the tablelist widget is "value1", not "value1 ". You can see this by inserting
puts [$entryW get]|
into the endICE proc, just before its return statement.
2. The
if {$val != $newVal} {check in the trimEntry proc won't work as expected if val is a string like "123 ", because the "==" and "!=" comparisons always try to treat their operands as numerical values. You should replace "!=" with "ne", or compare the string lengths instead.
3. The return statement without an argument in your startICE proc is equivalent to
return ""
which empties the entry widget when the editing begins. Is this the intended behavior? When not, you should replace it with
return $text
4. You have commented out the after idle statement in the checkLength procedure. This is also necessary, because in case of positive validation the entry will no longer exist at idle time. On the other hand, this is another argument for not doing the validation in this complicated way, and especially not when the entry loses focus. The best place for final validations is the command specified as the value of the -editendcommand option. These final validations are commonly performed without making use of the validation facilities of the entry widget.
5. This being said, I don't see any need for a -editstartcommand procedure and for using the entry validation mechanism in your example. You can do everything in endICE:
proc endICE {tablelist row col txt} {
if {[string length $txt] > 5} {
bell
$tablelist rejectinput
}
return $txt
}Or, if you want to be a little more tolerant, accepting trailing space characters in the entry but removing them from the string that gets inserted into the tablelist widget, you can change this proc as follows:
proc endICE {tablelist row col txt} {
set txt [string trim $txt]
if {[string length $txt] > 5} {
bell
$tablelist rejectinput
}
return $txt
}In both cases, you should create the tablelist widget without the -editstartcommand option:
::tablelist::tablelist .tl -editendcommand endICE
-- Csaba Nemethi http://www.nemethi.de mailto:csaba.nemethi@xxxxxxxxxxx .
- References:
- Validation and tablelist trouble
- From: mdn
- Validation and tablelist trouble
- Prev by Date: Re: ttk::treeview column grow setting?
- Next by Date: Re: Slightly OT: Incoming email triggers
- Previous by thread: Validation and tablelist trouble
- Next by thread: How does one modify existing font for ttk::label ?
- Index(es):
Relevant Pages
|