Source

xwork / docs / wikidocs / Building a Validator.html

<html>
    <head>
        <title>XWork : Building a Validator</title>
	    <link rel="stylesheet" href="styles/site.css" type="text/css" />
        <META http-equiv="Content-Type" content="text/html; charset=UTF-8">	    
    </head>

    <body>
	    <table class="pagecontent" border="0" cellpadding="0" cellspacing="0" width="100%" bgcolor="#ffffff">
		    <tr>
			    <td valign="top" class="pagebody">
				    <div class="pageheader">
					    <span class="pagetitle">
                            XWork : Building a Validator
                                                    </span>
				    </div>
				    <div class="pagesubheading">
					    This page last changed on Jun 03, 2004 by <font color="#0050B2">unkyaku</font>.
				    </div>

				    <p class="paragraph">Validators implement the <b class="strong">com.opensymphony.xwork.validator.Validator</b> interface</p><div class="code"><div class="codeContent">
<pre><span class="java&#45;keyword">public</span>&#160;<span class="java&#45;keyword">interface</span> Validator &#123;&#10;    void setDefaultMessage(<span class="java&#45;object">String</span> message);&#10;&#10;    <span class="java&#45;object">String</span> getDefaultMessage();&#10;&#10;    <span class="java&#45;object">String</span> getMessage(<span class="java&#45;object">Object</span> object);&#10;&#10;    void setMessageKey(<span class="java&#45;object">String</span> key);&#10;&#10;    <span class="java&#45;object">String</span> getMessageKey();&#10;&#10;    /&#42;&#42;&#10;     &#42; This method will be called before validate with a non&#45;<span class="java&#45;keyword">null</span> ValidatorContext.&#10;     &#42; &#64;param validatorContext&#10;     &#42;/&#10;    void setValidatorContext(ValidatorContext validatorContext);&#10;&#10;    ValidatorContext getValidatorContext();&#10;&#10;    /&#42;&#42;&#10;     &#42; The validation implementation must guarantee that setValidatorContext&#10;     &#42; will be called with a non&#45;<span class="java&#45;keyword">null</span> ValidatorContext before validate is called.&#10;     &#42; &#64;param object&#10;     &#42; &#64;<span class="java&#45;keyword">throws</span> ValidationException&#10;     &#42;/&#10;    void validate(<span class="java&#45;object">Object</span> object) <span class="java&#45;keyword">throws</span> ValidationException;&#10;&#125;</pre>
</div></div><p class="paragraph">FieldValidators implement <b class="strong">com.opensymphony.xwork.validator.FieldValidator</b>, which extends Validator:</p><div class="code"><div class="codeContent">
<pre><span class="java&#45;keyword">public</span>&#160;<span class="java&#45;keyword">interface</span> FieldValidator <span class="java&#45;keyword">extends</span> Validator &#123;&#10;&#10;    /&#42;&#42;&#10;     &#42; Sets the field name to validate with <span class="java&#45;keyword">this</span> FieldValidator&#10;     &#42; &#64;param fieldName&#10;     &#42;/&#10;    void setFieldName(<span class="java&#45;object">String</span> fieldName);&#10;&#10;    /&#42;&#42;&#10;     &#42; &#64;<span class="java&#45;keyword">return</span> the field name to be validated&#10;     &#42;/&#10;    <span class="java&#45;object">String</span> getFieldName();&#10;&#125;</pre>
</div></div><p class="paragraph">If you want to be able to use the &quot;short-circuit&quot; attribute, you should also implement <b class="strong">com.opensymphony.xwork.validator.ShortCircuitableValidator</b>.</p>Validators and FieldValidators can extend base classes <b class="strong">com.opensymphony.xwork.validator.validators.ValidatorSupport</b> and <b class="strong">com.opensymphony.xwork.validator.validators.FieldValidatorSupport</b> to get the base message and short-circuiting behavior, and will only need to implement validate(Action action). <p class="paragraph">The Support classes provide the functionality to use the message key and default message to get the localied message body and the parsing of the message body to provide for parameterized messages. Implementations of the Validator Interface which do not extend the Support base classes should provide this functionality as well for consistency.</p>The <b class="strong">ValidatorContext</b> set into the Validator is an interface which extends both <b class="strong">ValidationAware</b> and <b class="strong">LocaleAware</b> and is used for looking up message texts and settting errors. When validators are called from the ValidationInterceptor, a DelegatingValidatorContext is created which delegates these calls to the Action if it implements these interfaces. If the Action does not implement LocaleAware, a LocaleAwareSupport instance is created which uses the Action&#039;s class to look up resource bundle texts, if available. If the action does not implement ValidationAware, an implementation which simply logs the validation errors is created and delegated to. When calling the validation framework from outside the ValidationInterceptor, any ValidatorContext implementation can be passed in.<p class="paragraph">Validator classes may define any number of properties using the usual getX() setX() naming convention and have those properties set using &lt;param name=&quot;x&quot;&gt;foo&lt;/param&gt; elements below the &lt;validator&gt; element. The values of these properties may then be used in the validate() method to parameterize the validation. Validators which extend the Support classes may also use the</p><div class="code"><div class="codeContent">
<pre><span class="java&#45;object">Object</span> getFieldValue(<span class="java&#45;object">String</span> name, Action action)</pre>
</div></div><p class="paragraph">method to get the field value of a named property from an Action.</p>

				    
                    			    </td>
		    </tr>
	    </table>
	    <table border="0" cellpadding="0" cellspacing="0" width="100%">
			<tr>
				<td height="12" background="border/border_bottom.gif"><img src="border/spacer.gif" width="1" height="1" border="0"/></td>
			</tr>
		    <tr>
			    <td align="center"><font color="grey">Document generated by Confluence on Nov 14, 2004 17:31</font></td>
		    </tr>
	    </table>
    </body>
</html>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.