org.apache.tapestry.valid
Class BaseValidator

java.lang.Object
  extended by org.apache.tapestry.valid.BaseValidator
All Implemented Interfaces:
IValidator
Direct Known Subclasses:
DateValidator, EmailValidator, NumberValidator, PatternValidator, StringValidator, UrlValidator

public abstract class BaseValidator
extends Object
implements IValidator

Abstract base class for IValidator. Supports a required and locale property.

Since:
1.0.8
Version:
$Id: BaseValidator.java 243791 2004-02-19 17:38:13Z hlship $
Author:
Howard Lewis Ship

Field Summary
static String FIELD_SYMBOL
          Input Symbol used to represent the field being validated.
static String FORM_SYMBOL
          Input symbol used to represent the IForm containing the field to the script.
static String FUNCTION_SYMBOL
          Output symbol set by the script asthe name of the validator JavaScript function.
static String VALIDATOR_SYMBOL
          Input symbol used to represent the validator itself to the script.
 
Constructor Summary
  BaseValidator()
          Standard constructor.
protected BaseValidator(boolean required)
           
 
Method Summary
protected  String buildRequiredMessage(IFormComponent field)
          Builds an error message indicating a value for a required field was not supplied.
protected  boolean checkRequired(IFormComponent field, String value)
          Invoked to check if the value is null.
protected  String formatString(String pattern, Object arg)
          Convienience method for invoking formatString(String, Object[]).
protected  String formatString(String pattern, Object[] args)
          Gets a string from the standard resource bundle.
protected  String formatString(String pattern, Object arg1, Object arg2)
          Convienience method for invoking formatString(String, Object[]).
protected  String getPattern(String override, String key, Locale locale)
          Gets a pattern, either as the default value, or as a localized key.
 String getRequiredMessage()
           
 boolean isClientScriptingEnabled()
          Returns true if client scripting is enabled.
 boolean isRequired()
          All validators must implement a required property.
protected  void processValidatorScript(String scriptPath, IRequestCycle cycle, IFormComponent field, Map symbols)
          Invoked (from sub-class implementations of renderValidatorContribution(IFormComponent, IMarkupWriter, IRequestCycle) to process a standard validation script.
 void renderValidatorContribution(IFormComponent field, IMarkupWriter writer, IRequestCycle cycle)
          This implementation does nothing.
 void setClientScriptingEnabled(boolean clientScriptingEnabled)
           
 void setRequired(boolean required)
           
 void setRequiredMessage(String string)
          Overrides the field-is-required bundle key.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.apache.tapestry.valid.IValidator
toObject, toString
 

Field Detail

FIELD_SYMBOL

public static final String FIELD_SYMBOL
Input Symbol used to represent the field being validated.

Since:
2.2
See Also:
processValidatorScript(String, IRequestCycle, IFormComponent, Map), Constant Field Values

VALIDATOR_SYMBOL

public static final String VALIDATOR_SYMBOL
Input symbol used to represent the validator itself to the script.

Since:
2.2
See Also:
processValidatorScript(String, IRequestCycle, IFormComponent, Map), Constant Field Values

FORM_SYMBOL

public static final String FORM_SYMBOL
Input symbol used to represent the IForm containing the field to the script.

Since:
2.2
See Also:
processValidatorScript(String, IRequestCycle, IFormComponent, Map), Constant Field Values

FUNCTION_SYMBOL

public static final String FUNCTION_SYMBOL
Output symbol set by the script asthe name of the validator JavaScript function. The function implemented must return true or false (true if the field is valid, false otherwise). After the script is executed, the function is added to the IForm as a FormEventType.SUBMIT.

Since:
2.2
See Also:
processValidatorScript(String, IRequestCycle, IFormComponent, Map), Constant Field Values
Constructor Detail

BaseValidator

public BaseValidator()
Standard constructor. Leaves locale as system default and required as false.


BaseValidator

protected BaseValidator(boolean required)
Method Detail

isRequired

public boolean isRequired()
Description copied from interface: IValidator
All validators must implement a required property. If true, the client must supply a non-null value.

Specified by:
isRequired in interface IValidator

setRequired

public void setRequired(boolean required)

getPattern

protected String getPattern(String override,
                            String key,
                            Locale locale)
Gets a pattern, either as the default value, or as a localized key. If override is null, then the key from the org.apache.tapestry.valid.ValidationStrings ResourceBundle (in the specified locale) is used. The pattern can then be used with formatString(String, Object[]).

Why do we not just lump these strings into TapestryStrings.properties? because TapestryStrings.properties is localized to the server's locale, which is fine for the logging, debugging and error messages it contains. For field validation, whose errors are visible to the end user normally, we want to localize to the page's locale.

Parameters:
override - The override value for the localized string from the bundle.
key - used to lookup pattern from bundle, if override is null.
locale - used to get right localization of bundle.
Since:
3.0

formatString

protected String formatString(String pattern,
                              Object[] args)
Gets a string from the standard resource bundle. The string in the bundle is treated as a pattern for MessageFormat.format(java.lang.String, java.lang.Object[]).

Parameters:
pattern - string the input pattern to be used with MessageFormat.format(java.lang.String, java.lang.Object[]). It may contain replaceable parameters, {0}, {1}, etc.
args - the arguments used to fill replaceable parameters {0}, {1}, etc.
Since:
3.0

formatString

protected String formatString(String pattern,
                              Object arg)
Convienience method for invoking formatString(String, Object[]).

Since:
3.0

formatString

protected String formatString(String pattern,
                              Object arg1,
                              Object arg2)
Convienience method for invoking formatString(String, Object[]).

Since:
3.0

checkRequired

protected boolean checkRequired(IFormComponent field,
                                String value)
                         throws ValidatorException
Invoked to check if the value is null. If the value is null (or empty), but the required flag is set, then this method throws a ValidatorException. Otherwise, returns true if the value is null.

Throws:
ValidatorException

buildRequiredMessage

protected String buildRequiredMessage(IFormComponent field)
Builds an error message indicating a value for a required field was not supplied.

Since:
3.0

renderValidatorContribution

public void renderValidatorContribution(IFormComponent field,
                                        IMarkupWriter writer,
                                        IRequestCycle cycle)
This implementation does nothing. Subclasses may supply their own implementation.

Specified by:
renderValidatorContribution in interface IValidator
Since:
2.2

processValidatorScript

protected void processValidatorScript(String scriptPath,
                                      IRequestCycle cycle,
                                      IFormComponent field,
                                      Map symbols)
Invoked (from sub-class implementations of renderValidatorContribution(IFormComponent, IMarkupWriter, IRequestCycle) to process a standard validation script. This expects that:

Parameters:
scriptPath - the resource path of the script to execute
cycle - The active request cycle
field - The field to be validated
symbols - a set of input symbols needed by the script. These symbols are augmented with symbols for the field, form and validator. symbols may be null, but will be modified if not null.
Throws:
ApplicationRuntimeException - if there's an error processing the script.
Since:
2.2

isClientScriptingEnabled

public boolean isClientScriptingEnabled()
Returns true if client scripting is enabled. Some validators are capable of generating client-side scripting to perform validation when the form is submitted. By default, this flag is false and subclasses should check it (in renderValidatorContribution(IFormComponent, IMarkupWriter, IRequestCycle)) before generating client side script.

Since:
2.2

setClientScriptingEnabled

public void setClientScriptingEnabled(boolean clientScriptingEnabled)

getRequiredMessage

public String getRequiredMessage()

setRequiredMessage

public void setRequiredMessage(String string)
Overrides the field-is-required bundle key. Parameter {0} is the display name of the field.

Since:
3.0