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