Hi,
I've written a simple component that uses a regular expression to validate
input into a TextBox. Enclosed is the demo I created. If this seems to
be of help to you, let me know and maybe I'll create an example for the
website.
Billy
On Sunday, July 29, 2012 1:39:53 AM UTC-5, Maho wrote:
>
> Hi,
>
> Do we have any kind of validators in pyjs?
>
> By validator - I mean such mechanism, which is attached to user input
> (text box, date field, select, option, etc...) which checks if input is
> valid (valid field, state, number...) and if not, attach eg. red bulb
> with label to input saying "your input is wrong, please fix"
>
> If I need it, I should:
>
> 1. use what we have (I didn't find, but I'm still learning new things
> about pyjamaS),
> 2. port from gwt and use (could someone give me hint where to search it?)
> 3. write my own ....
>
> --
> pozdrawiam
>
> Łukasz Mach - [email protected]
>
>
--
from pyjamas.ui.TextBox import TextBox
from pyjamas import Window, DOM
from pyjamas.ui import Event
import re
class RegexTextBox(TextBox):
def __init__(self):
TextBox.__init__(self)
self._blurListeners=[]
self._invalidListeners=[]
self._validListeners=[]
self._regex=None
def setRegex(self, regex):
self._regex=regex
self._blurListeners.append(self.validate)
def validate(self):
if self._regex is None:
return
if re.match(self._regex, self.getText()):
_listeners=self._validListeners
else:
_listeners=self._invalidListeners
for _listener in _listeners:
_listener(self)
def appendInvalidListener(self, listener):
self._invalidListeners.append(listener)
def appendValidListener(self, listener):
self._validListeners.append(listener)
def onBlur(self):
self.validate()
def onBrowserEvent(self, event):
TextBox.onBrowserEvent(self, event)
type=DOM.eventGetType(event)
if type == "blur":
for _listener in self._blurListeners:
if hasattr(_listener, 'onBlur'):
_listener.onBlur(self)
else:
_listener(self)
import pyjd # dummy in pyjs
from pyjamas import Window, DOM
from pyjamas.ui.RootPanel import RootPanel
from RegexTextBox import RegexTextBox
def display_ok(obj):
obj.setStyleName("gwt-textbox-ok")
def display_error(obj):
obj.setStyleName("gwt-textbox-error")
class RegexTextBoxDemo:
def onModuleLoad(self):
rtb=RegexTextBox()
rtb.setRegex("^\d+$")
rtb.appendValidListener(display_ok)
rtb.appendInvalidListener(display_error)
RootPanel().add(rtb)
if __name__ == '__main__':
pyjd.setup("./public/RegexTextBoxDemo.html")
app = RegexTextBoxDemo()
app.onModuleLoad()
pyjd.run()