Pre-defined validators?

Hi all,

I'm thinking about adding validators to my controls, and I was just wondering if anyone has written up some of the common types of validators - e.g. for fields that must have a value, email fields, valid file/folder name (i.e. no unacceptable characters), etc. If they do exist, I think it'd be nice to get them into wxPython as I think these are the sorts of things that can be reused quite a bit. Otherwise, I'll create and contribute them if there's interest, but I just wanted to check that I wasn't re-inventing the wheel here. :slight_smile:

Thanks,

Kevin

Kevin Ollivier wrote:

...I was just wondering if anyone has written up some of the common
types of validators...

Simple ones only. I do like Phil's idea, though. These were written
before the wx namespace changeover, and I've yet to up-port the code,
but it may be useful regardless. Sadly I realize that these could be
refactored together for the most part, but it's part of a project that's
basically done and buried. :wink:

Here we go:

# Validator.py

from wxPython.wx import *
import string

class Number(wxPyValidator):
    def __init__(self):
        wxPyValidator.__init__(self)
        EVT_CHAR(self, self.OnChar)

    def Clone(self):
        return Number()

    def Validate(self, window):
        control = self.GetWindow()
        value = control.GetValue()
        for c in value:
            if c not in string.digits:
                return False
        return True

    def OnChar(self, event):
        key = event.KeyCode()
        if (chr(key) in string.digits) or \
           (key < WXK_SPACE) or (key == WXK_DELETE) or (key >= 255):
            # let the control handle the message,
            # the character typed is OK.
            event.Skip()
        elif not wxValidator_IsSilent():
            wxBell()

FILENAME_CHARACTERS = string.digits + string.letters + ' _-'

class Filename(wxPyValidator):
    def __init__(self):
        wxPyValidator.__init__(self)
        EVT_CHAR(self, self.OnChar)

    def Clone(self):
        return Filename()

    def Validate(self, window):
        control = self.GetWindow()
        value = control.GetValue()
        for c in value:
            if c not in FILENAME_CHARACTERS:
                return False
        return True

    def OnChar(self, event):
        key = event.KeyCode()
        if (key <= 255) and (chr(key) in FILENAME_CHARACTERS) or \
           (key < WXK_SPACE) or (key == WXK_DELETE):
            # let the control handle the message,
            # the character typed is OK.
            event.Skip()
        elif not wxValidator_IsSilent():
            wxBell()

-tom!

ยทยทยท

--