Application Techniques



Validating Data Entry in the Form and Its Controls

How to use validation events to check and correct data a user enters in a form.

About this technique

Details

Category

Java Client Techniques> General techniques for controls

Description

You'll learn about:

You can run this technique code from:

NOTE   First make sure that database is running on your localhost SilverStream Server

Related reading

See the section on forms and data validation in the Form Basics chapter in the Programmer's Guide

The frmValidateForm example illustrates how to use the validation events for a form. Four fields have validation code that calls the static methods for specifying validation failure, success, or correction (which corrects and succeeds). These static methods return an Exception object with appropriate settings. Your code throws this exception object to determine what happens next in the validation process.

Validation occurs at two different times. For individual fields, validationTest fires when the field loses focus. For the form, globalValidationTest fires when the user tries to leave the record. When happens next depends on which method you call to specify the status of the validation. For complete information, see the related reading in the Programmer's Guide.

In the example, there are four validation tests:

  1. This example requires that company codes be 6 chars or less and uppercase. Validation for the company code field checks the length and if too long, failure occurs. If the value is not uppercase, the correction() method converts it automatically without failing.

  2. The state field allows the user to type a 2-letter code or select the state name from a list. Validation for the state field checks that the user's typed code matches a state on the list. If there is no match, the code calls failure().

    NOTE   This example does not support letting the user type the full state name.

  3. For the country field, if left blank, the validation event uses the correction() method to insert a default value of USA.

  4. Global validation checks the country field and if it is USA, it checks the postal code to verify that it has a 5-digit or a 5+4 format.

Validation for individual fields   Top of page

Three fields on the form have code in their validationTest event.

For the company code field, the validationTest event throws a failure exception if the user's input is too long. This triggers the validationFailed event. If the first test passes, the code corrects the input to uppercase.

  private void 
   handle_fldCompanyCode_validationTest(AgoPropertyChangeEvent evt) 
       throws AgoValidationException 
  { 
     System.out.println("validationTest fldCompanyCode"); 
     String sVal = (String) evt.getNewValue(); 
     if (sVal.length() > 6) 
     { 
        // failure() triggers validationFailed 
        throw AgoValidationException.failure( 
           "The company code must be 6 characters or less."); 
     } 
   
     String sValUpper = sVal.toUpperCase(); 
     if (sValUpper != sVal) 
     { 
        // correction() causes the data to pass validation 
        throw AgoValidationException.correction(sValUpper); 
     } 
  } 

In the cboxStateid control, if the user has entered 2 characters, the code assumes it is the state code and uses it to set the value in the combo box. If the call to setValue() matched a valid code, then the following call to getSelectedIndex() will return a valid index number. If not, the code throws a validation failure with a message.

  private void 
    handle_cboxStateid_validationTest(AgoPropertyChangeEvent evt)  
         throws AgoValidationException 
  { 
     System.out.println("validationTest cboxStateid"); 
   
     String sVal = (String)evt.getNewValue(); 
     AgcJComboBox cbox = (AgcJComboBox) evt.getSource(); 
      
     // if the user enters a 2-char state code (list storage value),  
     // find the corresponding name (list display value) 
     if (sVal.length() == 2) 
     { 
        cbox.setValue(sVal); 
     } 
      
     // for any typed input, make sure it's a valid state 
     if (sVal.length() > 0)  
     { 
        int index = cbox.getSelectedIndex(); 
        if (index == -1) 
        { 
           throw AgoValidationException.failure( 
  "In the USA, enter a valid state code. Otherwise, leave blank."); 
        } 
     } 
  } 

NOTE   If the argument for setValue() doesn't match the storage value for an item on the list, getSelectedIndex() returns -1. A storage value is specified but is not an actual list item.

The cboxCountry control allows the user to select from a dropdown list. The validationTest event checks whether the value is blank, and if so, inserts the default value of USA. No validation failure occurs.

     private void handle_cboxCountry_validationTest(AgoPropertyChangeEvent evt) throws AgoValidationException 
  { 
     if (evt.getNewValue().equals("")) 
     { 
        throw AgoValidationException.correction("USA"); 
     } 
  } 

Finishing the validation process for controls   Top of page

In frmValidateForm, the validationFailed event occurs for bad values in fldCompanyCode and cboxStateid. The code for these events doesn't do anything fancy; it just displays a status message on the Java Console.

Because no further exceptions are thrown, the globalValidationFailed event occurs after validationFailed. Finally SilverStream displays a dialog box with the most recent error message for the validation exception.

If no exception is thrown, validationFailed fires globalValidationFailed and globalValidationFailed is followed by the error dialog box.

To halt prevent the validation process, which prevents the display of the error dialog box, your code can call success() and throw the returned exception object.

  throw AgoValidationException.success(); 

Global validation for the form   Top of page

In globalValidationTest, frmValidateForm verifies the format of the postal code if the country is USA. It is not appropriate to verify the postal code in control-level validation because the user may not have entered a country value yet.

Before globalValidationTest is fired, SilverStream fires validationTest for every control. During global validation, all data has passed the individual validation tests.

  public void globalValidationTest() throws AgoValidationException 
  { 
     System.out.println("globalValidationTest"); 
     String sCountry = (String) cboxCountry.getSelectedItem(); 
      
     // For USA, check that postal code has 5 or 5+4 digits 
     String msg = "In the USA, the zip code must be \n" 
        + "5 digits (xxxxx) or 5+4 digits (xxxxx-xxxx)."; 
         
     if (sCountry.equals("USA")) 
     { 
        String sPostal = fldPostalCode.getText(); 
        switch (sPostal.length() ) 
        { 
           case 5: // input is 5 chars; needs to be digits 
              try  
              { // try to convert to a number 
                 Integer iVal = Integer.valueOf(sPostal); 
              }  
              catch (NumberFormatException e) 
              { 
                 System.out.println( 
                    "Couldn't convert 5-char string to integer."); 
                 throw AgoValidationException.failure(msg); 
              }  
              break; 
   
           case 10: // input is 10 chars; needs to be 5+4 format 
              // 6th char must be a dash 
              if (sPostal.indexOf('-') != 5) 
              { 
                 System.out.println("Sixth char is not a dash."); 
                 throw AgoValidationException.failure(msg); 
              } 
              // get the 2 numeric substrings 
              String s1 = sPostal.substring(0,4); 
              String s2 = sPostal.substring(6,9); 
              try 
              { // try to convert each substring to a number 
                 Integer iVal; 
                 iVal = Integer.valueOf(s1); 
                 iVal = Integer.valueOf(s2); 
              }  
              catch (NumberFormatException e) 
              { 
                 System.out.println( 
                 "Couldn't convert 5-or 4-char string to integer."); 
                 throw AgoValidationException.failure(msg); 
              }                 
              break; 
           default: 
              System.out.println("Length is not 5 or 10"); 
              throw AgoValidationException.failure(msg); 
        } 
     } 
  } 

The globalValidationFailed event occurs after globalValidationTest if the postal code does not have a valid format. Then SilverStream displays a dialog box with the most recent error message for the validation exception.






Copyright © 2000, SilverStream Software, Inc. All rights reserved.