001// Copyright (c) 2001 Hursh Jain (http://www.mollypages.org) 002// The Molly framework is freely distributable under the terms of an 003// MIT-style license. For details, see the molly pages web site at: 004// http://www.mollypages.org/. Use, modify, have fun ! 005 006package fc.web.forms; 007 008import javax.servlet.*; 009import javax.servlet.http.*; 010import java.io.*; 011import java.util.*; 012 013import fc.jdbc.*; 014import fc.io.*; 015import fc.util.*; 016 017/** 018Represents a form field validator. Various subclasses 019implement concrete code (via {@link #validate}) for various 020validation strategies. Subclasses are typically specific to 021both the field type and the type of validation. So for 022example, the {@link ValidatorText} would verify that the 023specified HTML form element of type "text" has the specified 024length. 025<p> 026Field level validators differ from {@link FormValidator} 027because validation is at the field (per field) level and is 028independent of other fields in the form. 029<p> 030<b>Note</b>: Validator objects have state and a particular 031instance of a validator should only be assigned to one form 032field. To ensure this, validators take a field reference in 033their constructors and automatically add themselves to the 034specified field at construction time. During validation, 035validators are sequentially called in the order they were 036added (which implies in the order they were constructed). 037<p> 038<b>Thread safety:</b> None of the validation classes (like 039all other form-related classes) are thread safe. 040Synchronization must be done at a higher level, typically 041the session level. 042 043@author hursh jain 044**/ 045public abstract class FieldValidator 046{ 047protected String errorMessage; 048protected Field field; 049//protected boolean isOptional; 050 051/** 052Creates a new validator. 053 054@param field the field to validate. <b>This validator 055 is added to the field automatically. 056 Subclasses can later retrieve the field 057 for validation</b>. 058@param errorMessage the error message associated with invalid data 059 See {@link #getErrorMessage} 060**/ 061public FieldValidator(Field field, String errorMessage) 062 { 063 field.addValidator(this); 064 this.errorMessage = errorMessage; 065 this.field = field; 066 } 067 068/** 069Validates the field in some fashion. 070<p> 071If there are validation error, stores the error in the formdata 072and returns <tt>false</tt>, otherwise returns <tt>true</tt> 073*/ 074public abstract boolean validate(FormData fd, HttpServletRequest req); 075 076/* 077Makes this an optional validator for this field. During 078validation, optional validators attached to a field are invoked 079if and only if optional validation for that field is enabled. 080Optional validation is intended for cases where validation should 081only proceed based on certain states of the form (for example, 082certain choices in the form might imply that certain other parts 083of the form must be validated and those same parts must 084<b>not</b> be validated in other cases. 085*/ 086/* >>>>>>>>>>>>> disable/enable the field instead. 087public abstract setOptional(boolean val) { 088 this.isOptional = true; 089 } 090*/ 091 092public String getErrorMessage() { 093 return errorMessage; 094 } 095 096/** 097Returns the field associated with this validator. 098*/ 099public Field getField() { 100 return field; 101 } 102} //~class FieldValidator