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