org.apache.tapestry.valid
Class ValidationDelegate

java.lang.Object
  |
  +--org.apache.tapestry.valid.ValidationDelegate
All Implemented Interfaces:
IValidationDelegate

public class ValidationDelegate
extends Object
implements IValidationDelegate

A base implementation of IValidationDelegate that can be used as a helper bean. This class is often subclassed, typically to override presentation details.

Since:
1.0.5
Version:
$Id: ValidationDelegate.java,v 1.7 2004/02/19 17:38:03 hlship Exp $
Author:
Howard Lewis Ship

Constructor Summary
ValidationDelegate()
           
 
Method Summary
 void clear()
          Clears all tracking information.
protected  FieldTracking findCurrentTracking()
          Finds or creates the field tracking for the setFormComponent(IFormComponent) current component.
 List getAssociatedTrackings()
          Returns a List of IFieldTrackings.
protected  FieldTracking getComponentTracking()
          Returns the IFieldTracking for the current component, if any.
 String getFieldInputValue()
          Returns the string submitted by the client as the value for the current field.
 List getFieldTracking()
          Returns all the field trackings as an unmodifiable List.
 IRender getFirstError()
          A convienience, as most pages just show the first error on the page.
 boolean getHasErrors()
          Returns true if any form component has errors.
 List getUnassociatedTrackings()
          Like getAssociatedTrackings(), but returns only the unassociated trackings.
 boolean isInError()
          Returns true if the current component is in error (that is, had bad input submitted by the end user).
protected  boolean isInError(IFormComponent component)
          Checks to see if the field is in error.
 void record(IRender errorRenderer, ValidationConstraint constraint)
          Records error information about the currently selected component, or records unassociated (with any field) errors.
 void record(String message, ValidationConstraint constraint)
          Invokes record(IRender, ValidationConstraint), after wrapping the message parameter in a RenderString.
 void record(ValidatorException ex)
          Invokes record(String, ValidationConstraint), or record(IRender, ValidationConstraint) if the error renderer property is not null.
 void recordFieldInputValue(String input)
          Records the user's input for the current form component.
 void reset()
          Resets any tracking information for the current field.
 void setFormComponent(IFormComponent component)
          Invoked before other methods to configure the delegate for the given form component.
 void writeAttributes(IMarkupWriter writer, IRequestCycle cycle, IFormComponent component, IValidator validator)
          Does nothing.
 void writeLabelPrefix(IFormComponent component, IMarkupWriter writer, IRequestCycle cycle)
          If the form component is in error, places a <font color="red"< around it.
 void writeLabelSuffix(IFormComponent component, IMarkupWriter writer, IRequestCycle cycle)
          Closes the <font> element,started by writeLabelPrefix(IFormComponent,IMarkupWriter,IRequestCycle), if the form component is in error.
 void writePrefix(IMarkupWriter writer, IRequestCycle cycle, IFormComponent component, IValidator validator)
          Does nothing.
 void writeSuffix(IMarkupWriter writer, IRequestCycle cycle, IFormComponent component, IValidator validator)
          Default implementation; if the current field is in error, then a suffix is written.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ValidationDelegate

public ValidationDelegate()
Method Detail

clear

public void clear()
Description copied from interface: IValidationDelegate
Clears all tracking information.

Specified by:
clear in interface IValidationDelegate

writeLabelPrefix

public void writeLabelPrefix(IFormComponent component,
                             IMarkupWriter writer,
                             IRequestCycle cycle)
If the form component is in error, places a <font color="red"< around it. Note: this will only work on the render phase after a rewind, and will be confused if components are inside any kind of loop.

Specified by:
writeLabelPrefix in interface IValidationDelegate

writeLabelSuffix

public void writeLabelSuffix(IFormComponent component,
                             IMarkupWriter writer,
                             IRequestCycle cycle)
Closes the <font> element,started by writeLabelPrefix(IFormComponent,IMarkupWriter,IRequestCycle), if the form component is in error.

Specified by:
writeLabelSuffix in interface IValidationDelegate

getComponentTracking

protected FieldTracking getComponentTracking()
Returns the IFieldTracking for the current component, if any. The IFieldTracking is usually created in record(String, ValidationConstraint) or in record(IRender, ValidationConstraint).

Components may be rendered multiple times, with multiple names (provided by the Form, care must be taken that this method is invoked after the Form has provided a unique IFormComponent.getName() for the component.

Returns:
the FieldTracking, or null if the field has no tracking.
See Also:
setFormComponent(IFormComponent)

setFormComponent

public void setFormComponent(IFormComponent component)
Description copied from interface: IValidationDelegate
Invoked before other methods to configure the delegate for the given form component. Sets the current field based on the name of the form component (which is almost always a ValidField).

The caller should invoke this with a parameter of null to record unassociated global errors (errors not associated with any particular field).

Specified by:
setFormComponent in interface IValidationDelegate

isInError

public boolean isInError()
Description copied from interface: IValidationDelegate
Returns true if the current component is in error (that is, had bad input submitted by the end user).

Specified by:
isInError in interface IValidationDelegate

getFieldInputValue

public String getFieldInputValue()
Description copied from interface: IValidationDelegate
Returns the string submitted by the client as the value for the current field.

Specified by:
getFieldInputValue in interface IValidationDelegate

getFieldTracking

public List getFieldTracking()
Returns all the field trackings as an unmodifiable List.

Specified by:
getFieldTracking in interface IValidationDelegate

reset

public void reset()
Description copied from interface: IValidationDelegate
Resets any tracking information for the current field. This will clear the field's inError flag, and set its error message and invalid input value to null.

Specified by:
reset in interface IValidationDelegate

record

public void record(ValidatorException ex)
Invokes record(String, ValidationConstraint), or record(IRender, ValidationConstraint) if the error renderer property is not null.

Specified by:
record in interface IValidationDelegate

record

public void record(String message,
                   ValidationConstraint constraint)
Invokes record(IRender, ValidationConstraint), after wrapping the message parameter in a RenderString.

Specified by:
record in interface IValidationDelegate
Parameters:
message - message to display (@see RenderString}
constraint - the constraint that was violated, or null if not known

record

public void record(IRender errorRenderer,
                   ValidationConstraint constraint)
Records error information about the currently selected component, or records unassociated (with any field) errors.

Currently, you may have at most one error per field (note the difference between field and component), but any number of unassociated errors.

Subclasses may override the default error message (based on other factors, such as the field and constraint) before invoking this implementation.

Specified by:
record in interface IValidationDelegate
Parameters:
errorRenderer - object that will render the error message (@see RenderString}
constraint - the constraint that was violated, or null if not known
Since:
1.0.9

recordFieldInputValue

public void recordFieldInputValue(String input)
Description copied from interface: IValidationDelegate
Records the user's input for the current form component. Input should be recorded even if there isn't an explicit error, since later form-wide validations may discover an error in the field.

Specified by:
recordFieldInputValue in interface IValidationDelegate

findCurrentTracking

protected FieldTracking findCurrentTracking()
Finds or creates the field tracking for the setFormComponent(IFormComponent) current component. If no current component, an unassociated error is created and returned.

Since:
3.0

writePrefix

public void writePrefix(IMarkupWriter writer,
                        IRequestCycle cycle,
                        IFormComponent component,
                        IValidator validator)
Does nothing. Override in a subclass to decoreate fields.

Specified by:
writePrefix in interface IValidationDelegate

writeAttributes

public void writeAttributes(IMarkupWriter writer,
                            IRequestCycle cycle,
                            IFormComponent component,
                            IValidator validator)
Does nothing. Override in a subclass to decorate fields.

Specified by:
writeAttributes in interface IValidationDelegate

writeSuffix

public void writeSuffix(IMarkupWriter writer,
                        IRequestCycle cycle,
                        IFormComponent component,
                        IValidator validator)
Default implementation; if the current field is in error, then a suffix is written. The suffix is: &nbsp;<font color="red">**</font>.

Specified by:
writeSuffix in interface IValidationDelegate

getHasErrors

public boolean getHasErrors()
Description copied from interface: IValidationDelegate
Returns true if any form component has errors.

Specified by:
getHasErrors in interface IValidationDelegate

getFirstError

public IRender getFirstError()
A convienience, as most pages just show the first error on the page.

As of release 1.0.9, this returns an instance of IRender, not a String.


isInError

protected boolean isInError(IFormComponent component)
Checks to see if the field is in error. This will not work properly in a loop, but is only used by FieldLabel. Therefore, using FieldLabel in a loop (where the IFormComponent is renderred more than once) will not provide correct results.


getAssociatedTrackings

public List getAssociatedTrackings()
Returns a List of IFieldTrackings. This is the master list of trackings, except that it omits and trackings that are not associated with a particular field. May return an empty list, or null.

Order is not determined, though it is likely the order in which components are laid out on in the template (this is subject to change).


getUnassociatedTrackings

public List getUnassociatedTrackings()
Like getAssociatedTrackings(), but returns only the unassociated trackings. Unassociated trackings are new (in release 1.0.9), and are why interface IFieldTracking is not very well named.

The trackings are returned in an unspecified order, which (for the moment, anyway) is the order in which they were added (this could change in the future, or become more concrete).