Wednesday Feb 22, 2006

Simple Form Tutorial

Here is a very basic tutorial on creating a simple input form to add a record to a database. It leaves a lot to the reader. For example, you would want to have nicer messages for duplicate ids and you wouldn't want your error messages to display internal database information such as table names and column names. If the record was added sucessfully, you would probably want the page to navigate elsewhere.

1. Drop three Label components on the left side of the page, one under the other. Set the label text to ID:, Name:, and Description: respectively.

2. Drop three Text Field components on the page, one next to each label. Set the ids for the components to idTF, nameTF, and descriptionTF respectively.

4. Set the required property to true for each Text Field component.

6. Drop three Message components on the page, one next to each text field.

7. Ctrl-Shift-Drag from each Text Field component to its associated label.

8. Ctrl-Shift-Drag from each Text Field component to its associated message.

9. Drop a Button component on the page, under the text fields. Set its text to Add. Set its id to addButton.

10. Drop a Message Group component on the page to the right of the button. Set its showGlobalOnly property to true.

11. Access the Servers window. Drag Data Sources > Travel > Tables > TRIPTYPE and drop it on a blank spot on the page.

11. Double-click the button to access its addButton_action() method. Replace the method's body with the following code shown in bold.

    public String addButton_action() {
        if ( triptypeDataProvider.canAppendRow() ) {
            try {
                RowKey rowKey = triptypeDataProvider.appendRow();
                triptypeDataProvider.setCursorRow(rowKey);
                triptypeDataProvider.setValue("TRIPTYPE.TRIPTYPEID",
                        rowKey,  idTF.getText());
                triptypeDataProvider.setValue("TRIPTYPE.NAME",
                        rowKey, nameTF.getText());
                triptypeDataProvider.setValue("TRIPTYPE.DESCRIPTION",
                        rowKey, descriptionTF.getText());               
                triptypeDataProvider.commitChanges();

            } catch (Exception e) {
                error("Cannot append new trip type: " + e);
                triptypeDataProvider.refresh();
                return null;
            }
        } else {
            error("Cannot append trip types");
            return null;
        }
        //clear fields
        idTF.setText(null);
        nameTF.setText(null);  
        descriptionTF.setText(null);        
        return null;

12. Fix imports.

13. Run and test. It will reject existing IDs (1 through 10).

14. To see the database updates, right-click on the TRIPTYPE node in the Servers window and choose View Data.

About

divas

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today