The Velocity Project < Velocity Tools - Struts >

Velocity Tools

VelocityStruts

VelocityStruts Tools

Other Subprojects

ValidatorTool Reference Documentation
       

The ValidatorTool is used to interact with the Validator framework to generate dynamic javascript for form validation.

Class
 org.apache.velocity.tools.struts.ValidatorTool
Name
 $validator (this is the recommended name of the tool in the Velocity context)
Toolbox Configuration Example
 
<tool>
  <key>validator</key>
  <scope>request</scope>
  <class>org.apache.velocity.tools.struts.ValidatorTool</class>
</tool>
Author(s)
  Marino A. Jonsson
Method Overview
getPage() Gets the current page number of a multi-part form.
setPage() Sets the current page number of a multi-part form.
getMethod() Gets the method name that will be used for the javascript validation method name if it has a value.
setMethod() Sets the method name that will be used for the javascript validation method name if it has a value.
getHtmlComment() Gets whether or not to delimit the javascript with html comments.
setHtmlComment() Sets whether or not to delimit the javascript with html comments.
getSrc() Gets the src attribute's value when defining the html script element.
setSrc() Sets the src attribute's value (used to include an external script resource) when defining the html script element.
getCdata() Returns the cdata setting "true" or "false".
setCdata() Sets the cdata status.
getJavascript() Generates javascript to perform validations on a struts-defined form.
getDynamicJavascript() Generates the dynamic javascript methods to perform validation on a struts-defined form.
getStaticJavascript() Generates all the static javascript methods from validator-rules.xml.
See Also
The Javadoc for more info.
getPage()
       

Gets the current page number of a multi-part form.

int getPage()

Gets the current page number of a multi-part form. Only field validations with a matching page number will be generated that match the current page number.


setPage()
       

Sets the current page number of a multi-part form.

void setPage(int page)

Parameters
page
the current page number of a multi-part form.

Sets the current page number of a multi-part form. Only field validations with a matching page number will be generated that match the current page number.


getMethod()
       

Gets the method name that will be used for the javascript validation method name if it has a value.

String getMethod()

Gets the method name that will be used for the javascript validation method name if it has a value. This overrides the auto-generated method name based on the key (form name) passed in.


setMethod()
       

Sets the method name that will be used for the javascript validation method name if it has a value.

void setMethod(String methodName)

Parameters
methodName
the method name that will be used for the javascript validation method name.

Sets the method name that will be used for the javascript validation method name if it has a value. This overrides the auto-generated method name based on the key (form name) passed in.


getHtmlComment()
       

Gets whether or not to delimit the javascript with html comments.

boolean getHtmlComment()

Gets whether or not to delimit the javascript with html comments. If this is set to 'true', which is the default, html comments will surround the javascript.


setHtmlComment()
       

Sets whether or not to delimit the javascript with html comments.

void setHtmlComment(boolean htmlComment)

Parameters
htmlComment
whether or not to delimit the javascript with html comments.

Sets whether or not to delimit the javascript with html comments. If this is set to 'true', which is the default, html comments will surround the javascript.


getSrc()
       

Gets the src attribute's value when defining the html script element.

String getSrc()

Gets the src attribute's value when defining the html script element.


setSrc()
       

Sets the src attribute's value (used to include an external script resource) when defining the html script element.

void setSrc(String src)

Parameters
src
the src attribute's value.

Sets the src attribute's value (used to include an external script resource) when defining the html script element.


getCdata()
       

Returns the cdata setting "true" or "false".

boolean getCdata()

Returns the cdata setting "true" or "false".


setCdata()
       

Sets the cdata status.

void setCdata(boolean cdata)

Parameters
cdata
The cdata to set

Sets the cdata status.


getJavascript()
       

Generates javascript to perform validations on a struts-defined form.

String getJavascript()

String getJavascript(String formName)

Parameters
formName
The name of a struts-defined form.

Generates both dynamic and static javascript to perform validations on a struts-defined form. By default it uses the form associated with the action. A form name can also be supplied as a parameter to generate javascript for a struts-defined form that is not associated with this particular action.

Assuming that the name of a form associated with a given action is myForm, the following Velocity script:

<form action="/someAction.do" onsubmit="return validateMyForm(this)">
...
...
</form>

$validator.getJavascript()

produces something like this output (heavily abbreviated):

<form action="/someAction.do" onsubmit="return validateMyForm(this)">
...
...
</form>

<script type="text/javascript" language="Javascript1.1">
<!-- Begin

var bCancel = false;

function validateMyForm(form) {
	// call the relevant static methods to validate this form
	...
}

// some more dynamic functions dependent on which validations to perform.
...
...

// finally come all the static validator methods defined in validator-rules.xml
...
...

//End -->
</script>

getDynamicJavascript()
       

Generates the dynamic javascript methods to perform validation on a struts-defined form.

String getDynamicJavascript()

String getDynamicJavascript(String formName)

Parameters
formName
The name of a struts-defined form.

Generates the dynamic javascript to perform validations on a struts-defined form. By default it uses the form associated with the action. A form name can also be supplied as a parameter to generate javascript for a struts-defined form that is not associated with this particular action. This method does not work by itself - it requires that the relevant static methods be accessible i.e. in a seperate .js file.

Assuming that the name of a form associated with a given action is myForm, the following Velocity script:

<form action="/someAction.do" onsubmit="return validateMyForm(this)">
...
...
</form>

$validator.getDynamicJavascript()

produces something like this output (heavily abbreviated):

<form action="/someAction.do" onsubmit="return validateMyForm(this)">
...
...
</form>

<script type="text/javascript" language="Javascript1.1">
<!-- Begin

var bCancel = false;

function validateMyForm(form) {
	// call the relevant static methods to validate this form
	...
}

// some more dynamic functions dependent on which validations to perform.
...
...

//End -->
</script>

getStaticJavascript()
       

Generates all the static javascript methods from validator-rules.xml.

String getStaticJavascript()

Generates the static javascript methods from validator-rules.xml to perform validations on a any form. Useful i.e. if the static methods should be located in a seperate .js file.

The following Velocity script:

$validator.getStaticJavascript()

produces something like this output (heavily abbreviated):

<script type="text/javascript" language="Javascript1.1">
<!-- Begin

function validateFloatRange(form) {
	...
    	...
}

function validateByte(form) {
	...
    	...
}

// and a bunch more ...
...

//End -->
</script>


Copyright © 1999-2003, Apache Software Foundation