blob: dfe238c22f87c3116b8b98efd2c9a44c3d3b3816 [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- NewPage -->
<html lang="en">
<head>
<!-- Generated by javadoc (1.8.0_152) on Mon Apr 20 20:10:08 CEST 2020 -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ValidatorFactory (Struts 2 Core 2.6-SNAPSHOT API)</title>
<meta name="date" content="2020-04-20">
<link rel="stylesheet" type="text/css" href="../../../../stylesheet.css" title="Style">
<script type="text/javascript" src="../../../../script.js"></script>
</head>
<body>
<script type="text/javascript"><!--
try {
if (location.href.indexOf('is-external=true') == -1) {
parent.document.title="ValidatorFactory (Struts 2 Core 2.6-SNAPSHOT API)";
}
}
catch(err) {
}
//-->
var methods = {"i0":6,"i1":6,"i2":6};
var tabs = {65535:["t0","All Methods"],2:["t2","Instance Methods"],4:["t3","Abstract Methods"]};
var altColor = "altColor";
var rowColor = "rowColor";
var tableTab = "tableTab";
var activeTableTab = "activeTableTab";
</script>
<noscript>
<div>JavaScript is disabled on your browser.</div>
</noscript>
<!-- ========= START OF TOP NAVBAR ======= -->
<div class="topNav"><a name="navbar.top">
<!-- -->
</a>
<div class="skipNav"><a href="#skip.navbar.top" title="Skip navigation links">Skip navigation links</a></div>
<a name="navbar.top.firstrow">
<!-- -->
</a>
<ul class="navList" title="Navigation">
<li><a href="../../../../overview-summary.html">Overview</a></li>
<li><a href="package-summary.html">Package</a></li>
<li class="navBarCell1Rev">Class</li>
<li><a href="class-use/ValidatorFactory.html">Use</a></li>
<li><a href="package-tree.html">Tree</a></li>
<li><a href="../../../../deprecated-list.html">Deprecated</a></li>
<li><a href="../../../../index-all.html">Index</a></li>
<li><a href="../../../../help-doc.html">Help</a></li>
</ul>
</div>
<div class="subNav">
<ul class="navList">
<li><a href="../../../../com/opensymphony/xwork2/validator/ValidatorContext.html" title="interface in com.opensymphony.xwork2.validator"><span class="typeNameLink">Prev&nbsp;Class</span></a></li>
<li><a href="../../../../com/opensymphony/xwork2/validator/ValidatorFileParser.html" title="interface in com.opensymphony.xwork2.validator"><span class="typeNameLink">Next&nbsp;Class</span></a></li>
</ul>
<ul class="navList">
<li><a href="../../../../index.html?com/opensymphony/xwork2/validator/ValidatorFactory.html" target="_top">Frames</a></li>
<li><a href="ValidatorFactory.html" target="_top">No&nbsp;Frames</a></li>
</ul>
<ul class="navList" id="allclasses_navbar_top">
<li><a href="../../../../allclasses-noframe.html">All&nbsp;Classes</a></li>
</ul>
<div>
<script type="text/javascript"><!--
allClassesLink = document.getElementById("allclasses_navbar_top");
if(window==top) {
allClassesLink.style.display = "block";
}
else {
allClassesLink.style.display = "none";
}
//-->
</script>
</div>
<div>
<ul class="subNavList">
<li>Summary:&nbsp;</li>
<li>Nested&nbsp;|&nbsp;</li>
<li>Field&nbsp;|&nbsp;</li>
<li>Constr&nbsp;|&nbsp;</li>
<li><a href="#method.summary">Method</a></li>
</ul>
<ul class="subNavList">
<li>Detail:&nbsp;</li>
<li>Field&nbsp;|&nbsp;</li>
<li>Constr&nbsp;|&nbsp;</li>
<li><a href="#method.detail">Method</a></li>
</ul>
</div>
<a name="skip.navbar.top">
<!-- -->
</a></div>
<!-- ========= END OF TOP NAVBAR ========= -->
<!-- ======== START OF CLASS DATA ======== -->
<div class="header">
<div class="subTitle">com.opensymphony.xwork2.validator</div>
<h2 title="Interface ValidatorFactory" class="title">Interface ValidatorFactory</h2>
</div>
<div class="contentContainer">
<div class="description">
<ul class="blockList">
<li class="blockList">
<dl>
<dt>All Known Implementing Classes:</dt>
<dd><a href="../../../../com/opensymphony/xwork2/validator/DefaultValidatorFactory.html" title="class in com.opensymphony.xwork2.validator">DefaultValidatorFactory</a></dd>
</dl>
<hr>
<br>
<pre>public interface <span class="typeNameLabel">ValidatorFactory</span></pre>
<div class="block">ValidatorFactory
<p>
<!-- START SNIPPET: javadoc -->
Validation rules are handled by validators, which must be registered with
the ValidatorFactory (using the registerValidator method). The simplest way to do so is to add a file name
validators.xml in the root of the classpath (/WEB-INF/classes) that declares
all the validators you intend to use.
<!-- END SNIPPET: javadoc -->
</p>
<p>
<b>INFORMATION</b>
<!-- START SNIPPET: information -->
validators.xml if being defined should be available in the classpath. However
this is not necessary, if no custom validator is needed. Predefined sets of validators
will automatically be picked up when defined in
com/opensymphony/xwork2/validator/validators/default.xml packaged in
in the xwork jar file. See ValidatorFactory static block for details.
<!-- END SNIPPET: information -->
</p>
<p>
<b>WARNING</b>
<!-- START SNIPPET: warning -->
If custom validator is being defined and a validators.xml is created and
place in the classpath, do remember to copy all the other pre-defined validators
that is needed into the validators.xml as if not they will not be registered.
Once a validators.xml is detected in the classpath, the default one
(com/opensymphony/xwork2/validator/validators/default.xml) will not be loaded.
It is only loaded when a custom validators.xml cannot be found in the classpath.
Be careful.
<!-- END SNIPPET: warning -->
</p>
<p><b>Note:</b>
<!-- START SNIPPET: turningOnValidators -->
The default validationWorkflowStack already includes this.<br>
All that is required to enable validation for an Action is to put the
ValidationInterceptor in the interceptor refs of the action (see xwork.xml) like so:
<!-- END SNIPPET: turningOnValidators -->
</p>
<pre>
<!-- START SNIPPET: exTurnOnValidators -->
&lt;interceptor name="validator" class="com.opensymphony.xwork2.validator.ValidationInterceptor"/&gt;
<!-- END SNIPPET: exTurnOnValidators -->
</pre>
<p><b>Field Validators</b>
<!-- START SNIPPET: fieldValidators -->
Field validators, as the name indicate, act on single fields accessible through an action.
A validator, in contrast, is more generic and can do validations in the full action context,
involving more than one field (or even no field at all) in validation rule.
Most validations can be defined on per field basis. This should be preferred over
non-field validation wherever possible, as field validator messages are bound to the
related field and will be presented next to the corresponding input element in the
respecting view.
<!-- END SNIPPET: fieldValidators -->
</p>
<p><b>Non Field Validators</b>
<!-- START SNIPPET: nonFieldValidators -->
Non-field validators only add action level messages. Non-field validators
are mostly domain specific and therefore offer custom implementations.
The most important standard non-field validator provided by XWork
is ExpressionValidator.
<!-- END SNIPPET: nonFieldValidators -->
</p>
<p><b>NOTE:</b>
<!-- START SNIPPET: validatorsNote -->
Non-field validators takes precedence over field validators
regardless of the order they are defined in *-validation.xml. If a non-field
validator is short-circuited, it will causes its non-field validator to not
being executed. See validation framework documentation for more info.
<!-- END SNIPPET: validatorsNote -->
</p>
<p><b>VALIDATION RULES:</b></p>
<!-- START SNIPPET: validationRules1 -->
<p>Validation rules can be specified:</p>
<ol>
<li> Per Action class: in a file named ActionName-validation.xml</li>
<li> Per Action alias: in a file named ActionName-alias-validation.xml</li>
<li> Inheritance hierarchy and interfaces implemented by Action class:
XWork searches up the inheritance tree of the action to find default
validations for parent classes of the Action and interfaces implemented</li>
</ol>
<p>Here is an example for SimpleAction-validation.xml:</p>
<!-- END SNIPPET: validationRules1 -->
<pre>
<!-- START SNIPPET: exValidationRules1 -->
&lt;!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd"&gt;
&lt;validators&gt;
&lt;field name="bar"&gt;
&lt;field-validator type="required"&gt;
&lt;message&gt;You must enter a value for bar.&lt;/message&gt;
&lt;/field-validator&gt;
&lt;field-validator type="int"&gt;
&lt;param name="min"&gt;6&lt;/param&gt;
&lt;param name="max"&gt;10&lt;/param&gt;
&lt;message&gt;bar must be between ${min} and ${max}, current value is ${bar}.&lt;/message&gt;
&lt;/field-validator&gt;
&lt;/field&gt;
&lt;field name="bar2"&gt;
&lt;field-validator type="regex"&gt;
&lt;param name="expression"&gt;[0-9],[0-9]&lt;/param&gt;
&lt;message&gt;The value of bar2 must be in the format "x, y", where x and y are between 0 and 9&lt;/message&gt;
&lt;/field-validator&gt;
&lt;/field&gt;
&lt;field name="date"&gt;
&lt;field-validator type="date"&gt;
&lt;param name="min"&gt;12/22/2002&lt;/param&gt;
&lt;param name="max"&gt;12/25/2002&lt;/param&gt;
&lt;message&gt;The date must be between 12-22-2002 and 12-25-2002.&lt;/message&gt;
&lt;/field-validator&gt;
&lt;/field&gt;
&lt;field name="foo"&gt;
&lt;field-validator type="int"&gt;
&lt;param name="min"&gt;0&lt;/param&gt;
&lt;param name="max"&gt;100&lt;/param&gt;
&lt;message key="foo.range"&gt;Could not find foo.range!&lt;/message&gt;
&lt;/field-validator&gt;
&lt;/field&gt;
&lt;validator type="expression"&gt;
&lt;param name="expression"&gt;foo lt bar &lt;/param&gt;
&lt;message&gt;Foo must be greater than Bar. Foo = ${foo}, Bar = ${bar}.&lt;/message&gt;
&lt;/validator&gt;
&lt;/validators&gt;
<!-- END SNIPPET: exValidationRules1 -->
</pre>
<p>
<!-- START SNIPPET: validationRules2 -->
Here we can see the configuration of validators for the SimpleAction class.
Validators (and field-validators) must have a type attribute, which refers
to a name of an Validator registered with the ValidatorFactory as above.
Validator elements may also have &lt;param&gt; elements with name and value attributes
to set arbitrary parameters into the Validator instance. See below for discussion
of the message element.
<!-- END SNIPPET: validationRules2 -->
</p>
<!-- START SNIPPET: validationRules3 -->
<p>Each Validator or Field-Validator element must define one message element inside
the validator element body. The message element has 1 attributes, key which is not
required. The body of the message tag is taken as the default message which should
be added to the Action if the validator fails. Key gives a message key to look up
in the Action's ResourceBundles using getText() from LocaleAware if the Action
implements that interface (as ActionSupport does). This provides for Localized
messages based on the Locale of the user making the request (or whatever Locale
you've set into the LocaleAware Action). After either retrieving the message from
the ResourceBundle using the Key value, or using the Default message, the current
Validator is pushed onto the ValueStack, then the message is parsed for \$\{...\}
sections which are replaced with the evaluated value of the string between the
\$\{ and \}. This allows you to parameterize your messages with values from the
Validator, the Action, or both.</p>
<p>If the validator fails, the validator is pushed onto the ValueStack and the
message - either the default or the locale-specific one if the key attribute is
defined (and such a message exists) - is parsed for ${...} sections which are
replaced with the evaluated value of the string between the ${ and }. This
allows you to parameterize your messages with values from the validator, the
Action, or both. </p>
<p><b>NOTE:</b> Since validation rules are in an XML file, you must make sure
you escape special characters. For example, notice that in the expression
validator rule above we use "&amp;gt;" instead of "&gt;". Consult a resource on XML
for the full list of characters that must be escaped. The most commonly used
characters that must be escaped are: &amp; (use &amp;amp;), &gt; (user &amp;gt;), and &lt; (use &amp;lt;).</p>
<p>Here is an example of a parameterized message:</p>
<p>This will pull the min and max parameters from the IntRangeFieldValidator and
the value of bar from the Action.</p>
<!-- END SNIPPET: validationRules3 -->
<pre>
<!-- START SNIPPET: exValidationRules3 -->
bar must be between ${min} and ${max}, current value is ${bar}.
<!-- END SNIPPET: exValidationRules3 -->
</pre>
<!-- START SNIPPET: validationRules4 -->
<p>Another notable fact is that the provided message value is capable of containing OGNL expressions.
Keeping this in mind, it is possible to construct quite sophisticated messages.</p>
<p>See the following example to get an impression:</p>
<!-- END SNIPPET: validationRules4 -->
<pre>
<!-- START SNIPPET: exValidationRules4 -->
&lt;message&gt;${getText("validation.failednotice")}! ${getText("reason")}: ${getText("validation.inputrequired")}&lt;/message&gt;
<!-- END SNIPPET: exValidationRules4 -->
</pre></div>
<dl>
<dt><span class="simpleTagLabel">Author:</span></dt>
<dd>Jason Carreira, James House</dd>
</dl>
</li>
</ul>
</div>
<div class="summary">
<ul class="blockList">
<li class="blockList">
<!-- ========== METHOD SUMMARY =========== -->
<ul class="blockList">
<li class="blockList"><a name="method.summary">
<!-- -->
</a>
<h3>Method Summary</h3>
<table class="memberSummary" border="0" cellpadding="3" cellspacing="0" summary="Method Summary table, listing methods, and an explanation">
<caption><span id="t0" class="activeTableTab"><span>All Methods</span><span class="tabEnd">&nbsp;</span></span><span id="t2" class="tableTab"><span><a href="javascript:show(2);">Instance Methods</a></span><span class="tabEnd">&nbsp;</span></span><span id="t3" class="tableTab"><span><a href="javascript:show(4);">Abstract Methods</a></span><span class="tabEnd">&nbsp;</span></span></caption>
<tr>
<th class="colFirst" scope="col">Modifier and Type</th>
<th class="colLast" scope="col">Method and Description</th>
</tr>
<tr id="i0" class="altColor">
<td class="colFirst"><code><a href="../../../../com/opensymphony/xwork2/validator/Validator.html" title="interface in com.opensymphony.xwork2.validator">Validator</a></code></td>
<td class="colLast"><code><span class="memberNameLink"><a href="../../../../com/opensymphony/xwork2/validator/ValidatorFactory.html#getValidator-com.opensymphony.xwork2.validator.ValidatorConfig-">getValidator</a></span>(<a href="../../../../com/opensymphony/xwork2/validator/ValidatorConfig.html" title="class in com.opensymphony.xwork2.validator">ValidatorConfig</a>&nbsp;cfg)</code>
<div class="block">Get a Validator that matches the given configuration.</div>
</td>
</tr>
<tr id="i1" class="rowColor">
<td class="colFirst"><code><a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html?is-external=true" title="class or interface in java.lang">String</a></code></td>
<td class="colLast"><code><span class="memberNameLink"><a href="../../../../com/opensymphony/xwork2/validator/ValidatorFactory.html#lookupRegisteredValidatorType-java.lang.String-">lookupRegisteredValidatorType</a></span>(<a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html?is-external=true" title="class or interface in java.lang">String</a>&nbsp;name)</code>
<div class="block">Lookup to get the FQ classname of the given validator name.</div>
</td>
</tr>
<tr id="i2" class="altColor">
<td class="colFirst"><code>void</code></td>
<td class="colLast"><code><span class="memberNameLink"><a href="../../../../com/opensymphony/xwork2/validator/ValidatorFactory.html#registerValidator-java.lang.String-java.lang.String-">registerValidator</a></span>(<a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html?is-external=true" title="class or interface in java.lang">String</a>&nbsp;name,
<a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html?is-external=true" title="class or interface in java.lang">String</a>&nbsp;className)</code>
<div class="block">Registers the given validator to the existing map of validators.</div>
</td>
</tr>
</table>
</li>
</ul>
</li>
</ul>
</div>
<div class="details">
<ul class="blockList">
<li class="blockList">
<!-- ============ METHOD DETAIL ========== -->
<ul class="blockList">
<li class="blockList"><a name="method.detail">
<!-- -->
</a>
<h3>Method Detail</h3>
<a name="getValidator-com.opensymphony.xwork2.validator.ValidatorConfig-">
<!-- -->
</a>
<ul class="blockList">
<li class="blockList">
<h4>getValidator</h4>
<pre><a href="../../../../com/opensymphony/xwork2/validator/Validator.html" title="interface in com.opensymphony.xwork2.validator">Validator</a>&nbsp;getValidator(<a href="../../../../com/opensymphony/xwork2/validator/ValidatorConfig.html" title="class in com.opensymphony.xwork2.validator">ValidatorConfig</a>&nbsp;cfg)</pre>
<div class="block">Get a Validator that matches the given configuration.</div>
<dl>
<dt><span class="paramLabel">Parameters:</span></dt>
<dd><code>cfg</code> - the configurator.</dd>
<dt><span class="returnLabel">Returns:</span></dt>
<dd>the validator.</dd>
</dl>
</li>
</ul>
<a name="registerValidator-java.lang.String-java.lang.String-">
<!-- -->
</a>
<ul class="blockList">
<li class="blockList">
<h4>registerValidator</h4>
<pre>void&nbsp;registerValidator(<a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html?is-external=true" title="class or interface in java.lang">String</a>&nbsp;name,
<a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html?is-external=true" title="class or interface in java.lang">String</a>&nbsp;className)</pre>
<div class="block">Registers the given validator to the existing map of validators.
This will <b>add</b> to the existing list.</div>
<dl>
<dt><span class="paramLabel">Parameters:</span></dt>
<dd><code>name</code> - name of validator to add.</dd>
<dd><code>className</code> - the FQ classname of the validator.</dd>
</dl>
</li>
</ul>
<a name="lookupRegisteredValidatorType-java.lang.String-">
<!-- -->
</a>
<ul class="blockListLast">
<li class="blockList">
<h4>lookupRegisteredValidatorType</h4>
<pre><a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html?is-external=true" title="class or interface in java.lang">String</a>&nbsp;lookupRegisteredValidatorType(<a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html?is-external=true" title="class or interface in java.lang">String</a>&nbsp;name)</pre>
<div class="block">Lookup to get the FQ classname of the given validator name.</div>
<dl>
<dt><span class="paramLabel">Parameters:</span></dt>
<dd><code>name</code> - name of validator to lookup.</dd>
<dt><span class="returnLabel">Returns:</span></dt>
<dd>the found FQ classname</dd>
<dt><span class="throwsLabel">Throws:</span></dt>
<dd><code><a href="https://docs.oracle.com/javase/8/docs/api/java/lang/IllegalArgumentException.html?is-external=true" title="class or interface in java.lang">IllegalArgumentException</a></code> - is thrown if the name is not found.</dd>
</dl>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
<!-- ========= END OF CLASS DATA ========= -->
<!-- ======= START OF BOTTOM NAVBAR ====== -->
<div class="bottomNav"><a name="navbar.bottom">
<!-- -->
</a>
<div class="skipNav"><a href="#skip.navbar.bottom" title="Skip navigation links">Skip navigation links</a></div>
<a name="navbar.bottom.firstrow">
<!-- -->
</a>
<ul class="navList" title="Navigation">
<li><a href="../../../../overview-summary.html">Overview</a></li>
<li><a href="package-summary.html">Package</a></li>
<li class="navBarCell1Rev">Class</li>
<li><a href="class-use/ValidatorFactory.html">Use</a></li>
<li><a href="package-tree.html">Tree</a></li>
<li><a href="../../../../deprecated-list.html">Deprecated</a></li>
<li><a href="../../../../index-all.html">Index</a></li>
<li><a href="../../../../help-doc.html">Help</a></li>
</ul>
</div>
<div class="subNav">
<ul class="navList">
<li><a href="../../../../com/opensymphony/xwork2/validator/ValidatorContext.html" title="interface in com.opensymphony.xwork2.validator"><span class="typeNameLink">Prev&nbsp;Class</span></a></li>
<li><a href="../../../../com/opensymphony/xwork2/validator/ValidatorFileParser.html" title="interface in com.opensymphony.xwork2.validator"><span class="typeNameLink">Next&nbsp;Class</span></a></li>
</ul>
<ul class="navList">
<li><a href="../../../../index.html?com/opensymphony/xwork2/validator/ValidatorFactory.html" target="_top">Frames</a></li>
<li><a href="ValidatorFactory.html" target="_top">No&nbsp;Frames</a></li>
</ul>
<ul class="navList" id="allclasses_navbar_bottom">
<li><a href="../../../../allclasses-noframe.html">All&nbsp;Classes</a></li>
</ul>
<div>
<script type="text/javascript"><!--
allClassesLink = document.getElementById("allclasses_navbar_bottom");
if(window==top) {
allClassesLink.style.display = "block";
}
else {
allClassesLink.style.display = "none";
}
//-->
</script>
</div>
<div>
<ul class="subNavList">
<li>Summary:&nbsp;</li>
<li>Nested&nbsp;|&nbsp;</li>
<li>Field&nbsp;|&nbsp;</li>
<li>Constr&nbsp;|&nbsp;</li>
<li><a href="#method.summary">Method</a></li>
</ul>
<ul class="subNavList">
<li>Detail:&nbsp;</li>
<li>Field&nbsp;|&nbsp;</li>
<li>Constr&nbsp;|&nbsp;</li>
<li><a href="#method.detail">Method</a></li>
</ul>
</div>
<a name="skip.navbar.bottom">
<!-- -->
</a></div>
<!-- ======== END OF BOTTOM NAVBAR ======= -->
<p class="legalCopy"><small>Copyright &#169; 2000&#x2013;2020 <a href="http://www.apache.org">Apache Software Foundation</a>. All rights reserved.</small></p>
</body>
</html>