/examples/formpanel/FormPanelExample.py

http://pyjamas.googlecode.com/ · Python · 63 lines · 34 code · 13 blank · 16 comment · 1 complexity · 22e69f51e1895758112d4adb8f1d1f4d MD5 · raw file

  1. from ui import RootPanel, TextArea, Label, Button, HTML, VerticalPanel,\
  2. HorizontalPanel, ListBox, FormPanel, FileUpload, TextBox
  3. import Window
  4. class FormPanelExample:
  5. def onModuleLoad(self):
  6. # Create a FormPanel and point it at a service.
  7. self.form = FormPanel()
  8. self.form.setAction("/myFormHandler")
  9. # Because we're going to add a FileUpload widget, we'll need to set the
  10. # form to use the POST method, and multipart MIME encoding.
  11. self.form.setEncoding(FormPanel.ENCODING_MULTIPART)
  12. self.form.setMethod(FormPanel.METHOD_POST)
  13. # Create a panel to hold all of the form widgets.
  14. panel = VerticalPanel()
  15. self.form.setWidget(panel)
  16. # Create a TextBox, giving it a name so that it will be submitted.
  17. self.tb = TextBox()
  18. self.tb.setName("textBoxFormElement")
  19. panel.add(self.tb)
  20. # Create a ListBox, giving it a name and some values to be associated with
  21. # its options.
  22. lb = ListBox()
  23. lb.setName("listBoxFormElement")
  24. lb.addItem("foo", "fooValue")
  25. lb.addItem("bar", "barValue")
  26. lb.addItem("baz", "bazValue")
  27. panel.add(lb)
  28. # Create a FileUpload widget.
  29. upload = FileUpload()
  30. upload.setName("uploadFormElement")
  31. panel.add(upload)
  32. # Add a 'submit' button.
  33. panel.add(Button("Submit", self))
  34. # Add an event handler to the form.
  35. self.form.addFormHandler(self)
  36. RootPanel.get().add(self.form)
  37. def onClick(self, sender):
  38. self.form.submit()
  39. def onSubmitComplete(self, event):
  40. # When the form submission is successfully completed, this event is
  41. # fired. Assuming the service returned a response of type text/plain,
  42. # we can get the result text here (see the FormPanel documentation for
  43. # further explanation).
  44. Window.alert(event.getResults())
  45. def onSubmit(self, event):
  46. # This event is fired just before the form is submitted. We can take
  47. # this opportunity to perform validation.
  48. if (self.tb.getText().length == 0):
  49. Window.alert("The text box must not be empty")
  50. event.setCancelled(true)