Re: Validation and tablelist trouble



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
.



Relevant Pages

  • Re: regexp problems
    ... I tried it without the -textvariable in the entry widget, ... Then validation seems to become disabled <-- about this term, ... It seems that the quick keypress was so quick just before the dialog ...
    (comp.lang.tcl)
  • Data Validation Problem Work-Around needed
    ... If the result of adding the two numbers created a duplicate in column ... once the data involved in a specific entry has been ... Data validation would work perfectly for this problem if it could be ... But the programming to do this by brute force would be ...
    (microsoft.public.excel.misc)
  • Re: Data Validation Problem Work-Around needed
    ... If the result of adding the two numbers created a duplicate in column ... once the data involved in a specific entry has been ... Data validation would work perfectly for this problem if it could be ... But the programming to do this by brute force would be ...
    (microsoft.public.excel.misc)
  • RE: Can anyone povide step by step instructions on how to do the f
    ... Exit Sub ' can't match on no entry ... We then use the Cells() property of a worksheet to use those values from ... validated lists down to start below row 1. ... If Excel's Help on topics such as Data Validation, ...
    (microsoft.public.excel.misc)
  • RE: Can anyone povide step by step instructions on how to do the f
    ... Exit Sub ' can't match on no entry ... We then use the Cells() property of a worksheet to use those values from ... validated lists down to start below row 1. ... If Excel's Help on topics such as Data Validation, ...
    (microsoft.public.excel.misc)