15.6. Example

In this example, we override 2 methods (preInsert and preUpdate) to do some basic validation checking.

Example 15.3. Validation checking in interceptor

import org.dbforms.config.*;
import org.dbforms.event.*;
import java.sql.*;
import java.util.*;
import javax.servlet.http.*;

    public class CheckCustomerData 
        extends DbEventInterceptorSupport {

        private int 
        checkCustomer(FieldValues fieldValues) // method invented by developer    
            throws ValidationException {

                String lastName = (String) fieldValues.get("lastname").getFieldValue();

                String pCode = (String) fieldValues.get("pcode").getFieldValue();
                
                String city = (String) fieldValues.get("city").getFieldValue();
                
                // perform form-validation
                if (lastName == null || lastName.trim().length()==0 ||   
                    pCode == null || pCode.trim().length()==0 ||
                    city == null || city.trim().length()==0)  {

                        throw new ValidationException("Please fill out the form completely!"); } 
                
                else return GRANT_OPERATION;

            }


        public int 
        preInsert(HttpServletRequest request,  Table table, FieldValues fieldValues, 
         DbFormsConfig config, Connection con)
            throws ValidationException {

                
        return checkCustomer(fieldValues);
                
            }


        public int 
        preUpdate(HttpServletRequest request, Table table, FieldValues fieldValues, 
           DbFormsConfig config, Connection con)
            throws ValidationException {

                
        return checkCustomer(fieldValues);
            }


    }


      

Remarks:

There is an Exception -- org.dbforms.config.ValidationException which may be thrown to signal a message to the end-user. In fact, returning the integer constant DENY_OPERATION defined in DbEventInterceptor leads to similar results (but with an unspecific error message and no generic message)