Having a validator issue

I wrote a validator that doesn’t work. Can you give me some pointers as to how can I fix my validator to actually validate data?

Please note that I am passing my validator class to the validator argument of a wx.TextCtrl.

The code of my validator is here: http://bpaste.net/show/133570/

What does your validator do… is the question?
Write a validator that works.

···

#–wxPython Imports.

import wx

if ‘phoenix’ in wx.version():
PHOENIX = True
else:
PHOENIX = False

#-- Validator -------------------------#
ALPHA_ONLY = 1
DIGIT_ONLY = 2

#-- String ----------------------------#
LETTERS = ‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’
DIGITS = ‘0123456789’
HEXDIGITS = ‘0123456789abcdefABCDEF’
OCTDIGITS = ‘01234567’
ASCII_LETTERS = ‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’
ASCII_LOWERCASE = ‘abcdefghijklmnopqrstuvwxyz’
ASCII_UPPERCASE = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’
WHITESPACE = '\t\n\x0b\x0c\r ’
PUNCTUATION = ‘!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~’

#-- Math ------------------------------#
PI = 3.141592653589793

class Validator(wx.Validator):
def init(self, flag=None, pyVar=None):
wx.Validator.init(self)
self.flag = flag
self.Bind(wx.EVT_CHAR, self.OnChar)

def Clone(self):
    return Validator(self.flag)

def Validate(self, win):
    tc = self.GetWindow()
    val = tc.GetValue()

    if self.flag == ALPHA_ONLY:
        for x in val:
            if x not in LETTERS:
                return False

    elif self.flag == DIGIT_ONLY:
        for x in val:
            if x not in DIGITS:
                return False

    return True

def OnChar(self, event):
    key = event.GetKeyCode()
    print(key)

    if key < wx.WXK_SPACE or key == wx.WXK_DELETE or key > 255:
        event.Skip()
        return

    if self.flag == ALPHA_ONLY and chr(key) in LETTERS:
        event.Skip()
        return

    if self.flag == DIGIT_ONLY and chr(key) in DIGITS:
        event.Skip()
        return

    if PHOENIX:
        if not wx.Validator.IsSilent():
            wx.Bell()
    else:
        if not wx.Validator_IsSilent():
            wx.Bell()

    # Returning without calling event.Skip eats the event before it gets to
    # the text control
    return

def digitValidator():
gValidator = Validator(DIGIT_ONLY)
return gValidator

def alphaValidator():
gValidator = Validator(ALPHA_ONLY)
return gValidator

I use in the Clone method a return like this: "return InputValidator()"

How are you using the validator in your code?

wxpython.org/Phoenix/docs/html/validator_overview.html

Note that if you are not on a Dialog you need to call 'yourview.Validate()' yourself, this will then validate all the controls on 'yourview', assuming that your containers have style "WS_EX_VALIDATE_RECURSIVELY " defined - see above doc page.

Werner

···

On 19/09/2013 01:26, Bo�tjan Mejak wrote:

I wrote a validator that doesn't work. Can you give me some pointers as to how can I fix my validator to actually validate data?

Please note that I am passing my validator class to the validator argument of a wx.TextCtrl.

The code of my validator is here: http://bpaste.net/show/133570/

I totally forgot to tell you guys what my validator should actually validate.

I need to validate 2 text controls, both instances of a single-line wx.TextCtrl. For the first text control validate an input of a URL, possibly adding the “http://www.” part if not given by the user. And for the second text control validate only that it is not empty.

So do I need to write two separate validator classes or how should I tackle this particular challenge of mine?

I totally forgot to tell you guys what my validator should actually validate.

I need to validate 2 text controls, both instances of a single-line wx.TextCtrl. For the first text control validate an input of a URL, possibly adding the “http://www.” part if not given by the user. And for the second text control validate only that it is not empty.

So do I need to write two separate validator classes or how should I tackle this particular challenge of mine?

Well, a regular expression will help with the first. Google python re URL for the first.
Secondly a wxValidator is not needed for NoneTypes. Python does just fine with it’s truth checks.
Ex:
in OnText or OnKey event defs… do something like this… or add a truth check to the validator itself for custom functionality.

txtCtrlValue = tc.GetValue()
if txtCtrlVal:
# DoValidation stuff. in this case, the validator is already there so return, pass, or do whatever you want for a None value.

The code you linked to is the equivalent of…
MyValidatorActuallyDoesNothingAtAll = txtCtrl.GetValue() # should be empty… == None, ‘’, “”, , {}, etc…

···

On Thursday, September 19, 2013 3:45:11 AM UTC-5, Boštjan Mejak wrote:

if MyValidatorActuallyDoesNothingAtAll:
# please help me. or return or pass. or…
meh() # custom None handler func.

def Validate(self, window):
textctrl = self.GetWindow()
value = textctrl.GetValue()

Can you tell me why isn’t sufficient if we do the following…

def Validate(self, window):
value = window.GetValue() # ???

Wouldn’t the ‘window’ argument provide us the window/widget (in our case the text control) for which we want the validation to occur?

Doing a little bit of debugging, and/or reading the doc, I am sure
you could have found this yourself.
I just stepped through one of my validators and in WingIDE debugger
I see this:
window
<twcbsrc.libui.sized.SizedPanel; proxy of <Swig Object of type
‘wxPyPanel *’ at 0x75a6b50> >
self.GetWindow()
<twcbsrc.libui.searchctrl.SearchCtrl; proxy of <Swig Object of
type ‘wxPyControl *’ at 0x7513d78> >
So, you can see that ‘window’ provides the container and not the
widget to validate.
Werner

···

Hi,

  On 20/09/2013 01:10, Boštjan Mejak wrote:
      def

Validate(self, window):

        textctrl = self.GetWindow()

        value = textctrl.GetValue()
        Can you tell me why

isn’t sufficient if we do the following…

        def Validate(self,

window):

                      value =

window.GetValue() # ???

        Wouldn't the 'window'

argument provide us the window/widget (in our case the text
control) for which we want the validation to occur?

I found something while Googling:
def Validate(self, window):
widget = self.GetWindow()

widgetValue = widget.GetValue()

Now I need to know something. Does the GetWindow() method use the ‘window’ object internally to retrieve the actual widget that the validator is bound to?

A few moments of reflection should have answered that.� It cannot
possibly do so, because the GetWindow method does not have access to
the parameters of the calling function.

···

Bo�tjan Mejak wrote:

I found something while Googling:
def
Validate(self, window):
� � widget = self.GetWindow()

� � widgetValue = widget.GetValue()

          Now I need to know something. Does the

GetWindow() method use the ‘window’ object internally to
retrieve the actual widget that the validator is bound to?

-- Tim Roberts, Providenza & Boekelheide, Inc.

timr@probo.com

I’m having trouble understanding the existence of the ‘win’ parameter. If it’s not usable, why put it there?

Bo�tjan Mejak wrote:

I'm having trouble understanding the existence of the 'win' parameter.
If it's not usable, why put it there?

In case the validator needs to reference the container. Just because it doesn't have the value you are expecting does not make it unusable. There is self.GetWindow() for what you want.

···

--
Robin Dunn
Software Craftsman

I tried win.GetWindow() at first but that didn’t work. Since the ‘win’ parameter is a reference to the container, why do I need to use ‘self’ instead of ‘win’ to get to the desired widget/window?