fc.web.forms
Class VText

java.lang.Object
  extended by fc.web.forms.FieldValidator
      extended by fc.web.forms.VText

public final class VText
extends FieldValidator

Allows for various types of text validation. This class is meant to be used with text based input types such as subclasses of AbstractText


Constructor Summary
VText(AbstractText field, String errorMessage)
          Creates a new validator that by default only fails validation if the field's value is empty, that is to say, it's not filled by the user or is filled only with spaces (since spaces are removed before validation).
 
Method Summary
 VText allowEmpty(boolean allow)
          Checks to see if the field is required to be filled by the user.
 VText allowFloatingOnly()
          Ensures that the string is composed of only floating point number characters with optional leading/trailing blanks.
 VText allowIntegersOnly()
          Ensures that the string is composed of only integer characters, with optional leading/trailing blanks.
 VText setAllowedChars(String chars)
          Sets the characters allowed in this field.
 VText setAllowedPattern(Pattern pat)
          Sets the regular expression representing the allowed input.
 VText setLengthRange(int minlength, int maxlength)
          Checks to see if the number of chars in the field are between the minimum and maximum amount (both inclusive).
 VText setMaxSize(int maxlength)
          Checks to see if the number of chars in the field are between 0 and the specified maximum amount (inclusive).
 VText setMinSize(int minlength)
          Checks to see if the number of chars in the field are at least the minimum number (inclusive) specified by this method.
 VText setUnallowedChars(String chars)
          Sets the characters not allowed in this field.
 String toString()
           
 void trimSpaces(boolean val)
          If set to true, trims the string entered by the user before attempting to run further validation on it.
 boolean validate(FormData fd, javax.servlet.http.HttpServletRequest req)
          Validates the field in some fashion.
 
Methods inherited from class fc.web.forms.FieldValidator
getErrorMessage, getField
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

VText

public VText(AbstractText field,
             String errorMessage)
Creates a new validator that by default only fails validation if the field's value is empty, that is to say, it's not filled by the user or is filled only with spaces (since spaces are removed before validation). This can be changed via the trimSpaces(boolean) method.

Other methods in this class can be invoked for further kinds of validation.

Method Detail

trimSpaces

public void trimSpaces(boolean val)
If set to true, trims the string entered by the user before attempting to run further validation on it. Defaults to false. [Note: by default, AbstractText fields trim their values anyway so this method is kinda redundant]


allowEmpty

public VText allowEmpty(boolean allow)
Checks to see if the field is required to be filled by the user. If the field is allowed to be empty (not filled) and is empty, then validation succeeds and no further validation checks are done.

This is useful, for example, if a field is required to be either totally empty or filled with some sort of pattern as specified by setAllowedPattern(java.util.regex.Pattern). Note, however, that this method can get confusing if a VFilled validator is also attached to this field (which can be done automatically by database objects generated by Generate. In that case, 2 error messages will be shown to the user if the field is left blank - one for the attached VFilled validator and one for this one.

Parameters:
allow - true to allow for an empty field false otherwise (defaults to true).
Returns:
this object for method chaining convenience

allowIntegersOnly

public VText allowIntegersOnly()
Ensures that the string is composed of only integer characters, with optional leading/trailing blanks. This is a convenience method that sets the pattern to be "\s*\d*\s*"


allowFloatingOnly

public VText allowFloatingOnly()
Ensures that the string is composed of only floating point number characters with optional leading/trailing blanks. This is a convenience method that sets the pattern to be "\s*-?\d*\.?\d*\s*"


setLengthRange

public VText setLengthRange(int minlength,
                            int maxlength)
Checks to see if the number of chars in the field are between the minimum and maximum amount (both inclusive). If the minimum and maximum amounts are the same (including 0), then the field has to be exactly that length. (empty fields are also allowed if set via allowEmpty method.

Returns:
this object for method chaining convenience

setMaxSize

public VText setMaxSize(int maxlength)
Checks to see if the number of chars in the field are between 0 and the specified maximum amount (inclusive). [this method calls setLengthRange(0, maxlength)]

Returns:
this object for method chaining convenience

setMinSize

public VText setMinSize(int minlength)
Checks to see if the number of chars in the field are at least the minimum number (inclusive) specified by this method.

Returns:
this object for method chaining convenience

setUnallowedChars

public VText setUnallowedChars(String chars)
Sets the characters not allowed in this field. If some character is marked as both allowed (via the setAllowedChars method) and unallowed (via this method), then unallowed characters have precedence and if found in the input, the field will not be validated.

The same functionality can be achieved via regular expressions and negated character classes. It's a matter of preference.

After this method is called, the pattern previously set (if any) via the setAllowedPattern(java.util.regex.Pattern) method will be ignored for validation.

Parameters:
chars - the unallowed chars. This parameter must not be null.
Returns:
this object for method chaining convenience

setAllowedChars

public VText setAllowedChars(String chars)
Sets the characters allowed in this field. All characters in the specified string will be allowed, all else disallowed. An empty string (no characters at all) is allowed at validation time if set via allowEmpty method.

After this method is called, the pattern previously set (if any) via the setAllowedPattern(java.util.regex.Pattern) method will be ignored for validation.

Parameters:
chars - the allowed chars. This parameter must not be null.
Returns:
this object for method chaining convenience

setAllowedPattern

public VText setAllowedPattern(Pattern pat)
Sets the regular expression representing the allowed input. The pattern will be matched with the entire value of the field.

After this method is called, the string previously set (if any) via the setAllowedChars(java.lang.String) method will be ignored for validation.

Parameters:
pat - the allowed pattern. Must not be null.
Returns:
this object for method chaining convenience

toString

public String toString()
Overrides:
toString in class Object

validate

public boolean validate(FormData fd,
                        javax.servlet.http.HttpServletRequest req)
Description copied from class: FieldValidator
Validates the field in some fashion.

If there are validation error, stores the error in the formdata and returns false, otherwise returns true

Specified by:
validate in class FieldValidator