blob: 34553a965b0b6b3ee5ac640768d704c2e4432520 [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- $Id$ -->
<html>
<head>
<title>ValidField</title>
<link rel="stylesheet" type="text/css" href="Tapestry.css" title="style">
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left"><A href="Upload.html"><IMG alt=Upload src="common-images/prev.png"></a></td>
<td align="right"><A href="index.html"><IMG alt="Component Index" src="common-images/home.png" ></a></td>
<tr>
<tr>
<td colspan="2"><hr></td>
</tr>
<tr>
<td colspan="2">
<table border="0" cellpadding="4" cellspacing="4" width="100%">
<tr valign="top">
<td>
<table>
<tr>
<td><font size="+2"><b>ValidField</b></font></td>
</tr>
<tr>
<td>
<A href="../api/org/apache/tapestry/valid/ValidField.html">
org.apache.tapestry.valid.ValidField</a>
</td>
</tr>
</table>
</td>
<td>
<table align="right" valign="middle" bgcolor="#c0c0c0" cellpadding="8">
<tr>
<td><b>Age</b></td><td><input style="BACKGROUND: yellow"></td>
</tr>
</table>
</td>
</tr>
<tr valign="center">
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td colspan="2">
<b>Description</b>
<br>
Provides an extended <a href="TextField.html">TextField</a> that performs
automated validation and conversion of user entered text into typed Objects.
The ValidField component is rendered as a HTML &lt;input type="text"&gt; field
and must be wrapped by a <a href="Form.html">Form</a> component.
<p>
Each ValidField is configured with a
<A href="../api/org/apache/tapestry/valid/IValidator.html">validator</a> which
checks the correctness of the user's input and performs the input text to
java Object translations. The <a href="Form.html">Form</a> tracks the state of
all its ValidFields using a validation
<a href="../api/org/apache/tapestry/valid/ValidationDelegate.html">delegate</a>.
Using the validation delegate you can determine whether all the form's fields
are valid and inform the user of any validation errors.
<p>
There are number of pluggable validators you can assign to a ValidField
including:
<ul>
<li>
<A href="../api/org/apache/tapestry/valid/DateValidator.html">DateValidator</a>
- validates date strings and allows a minimum and maximum date range to be set
</li>
<li>
<A href="../api/org/apache/tapestry/valid/EmailValidator.html">EmailValidator</a>
- validates email strings and allows a minimum and maximum length to be set
</li>
<li>
<A href="../api/org/apache/tapestry/valid/NumberValidator.html">NumberValidator</a>
- validates number strings and allows a minimum and maximum values to be set
</li>
<li>
<A href="../api/org/apache/tapestry/valid/PatternValidator.html">PatternValidator</a>
- validates text for a specified pattern
</li>
<li>
<A href="../api/org/apache/tapestry/valid/StringValidator.html">StringValidator</a>
- validates string values and allows a minimum length to be set
</li>
</ul>
The ValidField's
<A href="../api/org/apache/tapestry/valid/BaseValidator.html">BaseValidator</a>
classes automatically provide powerful client-side JavaScript field validation
when their parent <a href="Form.html">Form</a> is posted.
Validator client scripting is disabled by default. To enable the client
scripting explicitly set the "clientScriptingEnabled" property to true in
each validator. For example:
<pre>
&lt;bean name="emailValidator" class="org.apache.tapestry.valid.EmailValidator"&gt;
&lt;set-property name="<b>clientScriptingEnabled</b>" expression="true"/&gt;
&lt;set-property name="minimumLength" expression="8"/&gt;
&lt;set-property name="required" expression="true"/&gt;
&lt;/bean&gt;
</pre>
<p>
Note if a form includes a "Cancel" style submit button, the cancel button will
need to set the form's <tt>onsubmit</tt> event handler to null, so that the
JavaScript field validation is not invoked when the form is submitted.
For example:
<pre>
&lt;input jwcid="@<a href="Submit.html">Submit</a>" listener="ognl:listeners.okSubmit" type="Submit" value=" OK "/&gt;
&lt;input jwcid="@<a href="Submit.html">Submit</a>" listener="ognl:listeners.cancelSubmit" type="Submit" value="Cancel" <b>onclick</b>="form.onsubmit = null;"/&gt;
</pre>
</td>
</tr>
<tr>
<td colspan="2">
<b>See Also</b>
<br>
<A href="FieldLabel.html">FieldLabel</a>,
<A href="Form.html">Form</a>,
<a href="TextArea.html">TextArea</a>,
<A href="TextField.html">TextField</a>
</td>
</tr>
<tr>
<td colspan="2">
<b>Parameters</b>
<br>
<table border=1 cellpadding="4" cellspacing="4" class="parameters">
<tr>
<th>Name</th>
<th>Type</th>
<th>Direction</th>
<th>Required</th>
<th>Default</th>
<th>Description</th>
</tr>
<tr>
<td>value</td>
<td>Object</td>
<td>in-out</td>
<td>yes</td>
<td>&nbsp;</td>
<td>
The value to be displayed (on render), and updated (when the form is
submitted, if the submitted value is valid). The
<a href="../api/org/apache/tapestry/valid/IValidator.html"><tt>IValidator</tt></a>
converts between object values and Strings.
</td>
</tr>
<tr>
<td>validator</td>
<td>
<A href="../api/org/apache/tapestry/valid/IValidator.html"><tt>IValidator</tt></a>
</td>
<td>in</td>
<td>yes</td>
<td>&nbsp;</td>
<td>
Object used to convert object values to Strings (for renderring)
and to validate and convert Strings into object values (when the form
is submitted).
</td>
</tr>
<tr>
<td>displayName</td>
<td>String</td>
<td>in</td>
<td>yes</td>
<td>&nbsp;</td>
<td>
A textual name for the field that is used when formulating error messages.
Also used by the <A href="FieldLabel.html">FieldLabel</a> component to
properly label the field.
</td>
</tr>
<tr>
<td>hidden</td>
<td>boolean</td>
<td>in</td>
<td>no</td>
<td>false</td>
<td>
If true, then the text field is written as a &lt;input type="password"&gt;
form element.
</td>
</tr>
<tr>
<td>disabled</td>
<td>boolean</td>
<td>in</td>
<td>no</td>
<td>false</td>
<td>
Controls whether the text field is active or not. If disabled, then
any value that comes up when the form is submitted is ignored. Corresponds
to the "disabled" HTML attribute.
</td>
</tr>
</table>
<p>Body: <strong>removed</strong><br>
Informal parameters:<strong>allowed</strong><br>
Reserved parameters: "name", "type", "value"
</p>
</td>
</tr>
<tr>
<td colspan="2">
<b>Examples</b>
<p>
The ValidField is used in this example to provide an auction bidding page. A
custom
<a href="../api/org/apache/tapestry/valid/ValidationDelegate.html"><tt>ValidationDelegate</tt></a>
is used to render the invalid fiels as yellow. A custom ShowError component
is also used to display the first error message if any fields have errors.
<p>
For a more extensive example see the
<a href="../Tutorial/workbench.html">Tapestry Workbench</a>. The Fields
example in the Workbench also illustrates how to use the
<a href="FieldLabel.html">FieldLabel</a> component to high light invalid
fields.
<p>
<form>
<table class="examples" cellpadding="2">
<tr>
<td colspan="2"><font color="navy" size="+2"><i><b>Regal Auctions Bid Page</b></i></font></td>
</tr>
<tr>
<td colspan="2"><hr></td>
</tr>
<tr>
<td colspan="2"><font color="red">Bid Amount must be greater than 10.0</font></td>
</tr>
<tr>
<td>Lot Number</td><td><input type="text" size="4" maxlength="4" value="23"/></td>
</tr>
<tr>
<td>Bid Amount</td><td><input type="text" size="7" maxlength="7" value="9.99" style="background:yellow"/></td>
</tr>
<tr>
<td>Full Name</td><td><input type="text" size="25" maxlength="30" value="Peter McHolder"/></td>
</tr>
<tr>
<td>Email</td><td><input type="text" size="25" maxlength="30" value="pmcholder2@hotmail.com"/></td>
</tr>
<tr>
<td>Telphone</td><td><input type="text" size="25" maxlength="12" style="background:yellow"/></td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="Submit" value=" OK ">
<input type="Submit" value="Cancel">
</td>
</tr>
</table>
</form>
<pre>
&lt;form jwcid=&quot;@<A href="Form.html">Form</A>&quot; delegate=&quot;ognl:beans.delegate&quot;&gt;<br>&lt;table class=&quot;examples&quot; cellpadding=&quot;2&quot;&gt;<br>&lt;tr&gt;<br> &lt;td colspan=&quot;2&quot;&gt;&lt;span class=&quot;title&quot;&gt;Regal Auctions Bid Page&lt;/span&gt;&lt;/td&gt;<br>&lt;/tr&gt;<br>&lt;tr&gt;<br> &lt;td colspan=&quot;2&quot;&gt;&lt;hr&gt;&lt;/td&gt;<br>&lt;/tr&gt;<br>&lt;tr&gt;<br> &lt;td colspan=&quot;2&quot;&gt;&lt;span jwcid=&quot;@ShowError&quot; delegate=&quot;ognl:beans.delegate&quot;/&gt;&lt;/td&gt;<br>&lt;/tr&gt;<br>&lt;tr&gt;<br> &lt;td&gt;&lt;span jwcid=&quot;@<A href="FieldLabel.html">FieldLabel</A>&quot; field=&quot;ognl:components.lotNoField&quot;&gt;Lot Number&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&lt;input jwcid=&quot;lotNoField&quot; type=&quot;text&quot; size=&quot;4&quot; maxlength=&quot;4&quot;/&gt;&lt;/td&gt;<br>&lt;/tr&gt;<br>&lt;tr&gt;<br> &lt;td&gt;&lt;span jwcid=&quot;@<A href="FieldLabel.html">FieldLabel</A>&quot; field=&quot;ognl:components.bidAmountField&quot;&gt;Bid Amount&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&lt;input jwcid=&quot;bidAmountField&quot; type=&quot;text&quot; size=&quot;7&quot; maxlength=&quot;7&quot;/&gt;&lt;/td&gt;<br>&lt;/tr&gt;<br>&lt;tr&gt;<br> &lt;td&gt;&lt;span jwcid=&quot;@<A href="FieldLabel.html">FieldLabel</A>&quot; field=&quot;ognl:components.fullNameField&quot;&gt;Full Name&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&lt;input jwcid=&quot;fullNameField&quot; type=&quot;text&quot; size=&quot;25&quot; maxlength=&quot;30&quot;/&gt;&lt;/td&gt;<br>&lt;/tr&gt;<br>&lt;tr&gt;<br> &lt;td&gt;&lt;span jwcid=&quot;@<A href="FieldLabel.html">FieldLabel</A>&quot; field=&quot;ognl:components.emailField&quot;&gt;Email&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&lt;input jwcid=&quot;emailField&quot; type=&quot;text&quot; size=&quot;25&quot; maxlength=&quot;30&quot;/&gt;&lt;/td&gt;<br>&lt;/tr&gt;<br>&lt;tr&gt;<br> &lt;td&gt;&lt;span jwcid=&quot;@<A href="FieldLabel.html">FieldLabel</A>&quot; field=&quot;ognl:components.telephoneField&quot;&gt;Telephone&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&lt;input jwcid=&quot;telephoneField&quot; type=&quot;text&quot; size=&quot;25&quot; maxlength=&quot;30&quot;/&gt;&lt;/td&gt;<br>&lt;/tr&gt;<br>&lt;tr&gt;<br> &lt;td colspan=&quot;2&quot; align=&quot;right&quot;&gt;<br> &lt;input jwcid=&quot;@Submit&quot; listener=&quot;ognl:listeners.okSubmit&quot; type=&quot;Submit&quot; value=&quot; OK &quot;/&gt;<br> &lt;input jwcid=&quot;@Submit&quot; listener=&quot;ognl:listeners.cancelSubmit&quot;onclick=&quot;form.onsubmit = null;&quot;/&gt;<br> &lt;/td&gt;<br>&lt;/tr&gt;<br>&lt;/table&gt;<br>&lt;/form&gt;<br>
&lt;property-specification name=&quot;lotBid&quot; type=&quot;com.mycorp.LotBid&quot; persistent=&quot;yes&quot; initial-value=&quot;new com.mycorp.LotBid()&quot;/&gt;<br>&lt;property-specification name=&quot;actionDetails&quot; type=&quot;com.mycorp.ActionDetails&quot; persistent=&quot;yes&quot;/&gt;<br>
&lt;bean name="delegate" class="com.mycorp.FormValidationDelegate"/&gt;
&lt;bean name="lotNoValidator" class="<A href="../api/org/apache/tapestry/valid/NumberValidator.html">org.apache.tapestry.valid.NumberValidator</a>"&gt;
&lt;set-property name="required" expression="true"/&gt;
&lt;set-property name="minimum" expression="1"/&gt;
&lt;set-property name="maximum" expression="auctionDetails.numberLots"/&gt;
&lt;set-property name="valueType"&gt;"int"&lt;/set-property&gt;
&lt;/bean&gt;
&lt;bean name="bidAmountValidator" class="<A href="../api/org/apache/tapestry/valid/NumberValidator.html">org.apache.tapestry.valid.NumberValidator</a>"&gt;
&lt;set-property name="required" expression="true"/&gt;
&lt;set-property name="minimum" expression="auctionDetails.minBid"/&gt;
&lt;set-property name="maximum" expression="auctionDetails.maxBid"/&gt;
&lt;set-property name="valueType"&gt;"float"&lt;/set-property&gt;
&lt;/bean&gt;
&lt;bean name="fullNameValidator" class="<A href="../api/org/apache/tapestry/valid/StringValidator.html">org.apache.tapestry.valid.StringValidator</a>"&gt;
&lt;set-property name="required" expression="true"/&gt;
&lt;set-property name="minimumLength" expression="3"/&gt;
&lt;/bean&gt;
&lt;bean name="emailValidator" class="<A href="../api/org/apache/tapestry/valid/StringValidator.html">org.apache.tapestry.valid.StringValidator</a>"&gt;
&lt;set-property name="required" expression="true"/&gt;
&lt;set-property name="minimumLength" expression="12"/&gt;
&lt;/bean&gt;
&lt;bean name="telephoneValidator" class="<A href="../api/org/apache/tapestry/valid/StringValidator.html">org.apache.tapestry.valid.StringValidator</a>"&gt;
&lt;set-property name="required" expression="true"/&gt;
&lt;set-property name="minimumLength" expression="11"/&gt;
&lt;/bean&gt;
&lt;component id="<span class="jwcid">lotNoField</span>" type="<A href="ValidField.html">ValidField</a>"&gt;
&lt;binding name="value" expression="lotBid.lotNo"/&gt;
&lt;binding name="validator" expression="beans.lotNoValidator"/&gt;
&lt;static-binding name="displayName" value="Lot Number"/&gt;
&lt;/component&gt;
&lt;component id="<span class="jwcid">bidAmountField</span>" type="<A href="ValidField.html">ValidField</a>"&gt;
&lt;binding name="value" expression="lotBid.bidAmount"/&gt;
&lt;binding name="validator" expression="beans.bidAmountNoValidator"/&gt;
&lt;static-binding name="displayName" value="Bid Amount"/&gt;
&lt;/component&gt;
&lt;component id="<span class="jwcid">fullNameField</span>" type="<A href="ValidField.html">ValidField</a>"&gt;
&lt;binding name="value" expression="lotBid.bidderName"/&gt;
&lt;binding name="validator" expression="beans.fullNameValidator"/&gt;
&lt;static-binding name="displayName" value="Full Name"/&gt;
&lt;/component&gt;
&lt;component id="<span class="jwcid">emailField</span>" type="<A href="ValidField.html">ValidField</a>"&gt;
&lt;binding name="value" expression="lotBid.bidderEmail"/&gt;
&lt;binding name="validator" expression="beans.emailValidator"/&gt;
&lt;static-binding name="displayName" value="Email"/&gt;
&lt;/component&gt;
&lt;component id="<span class="jwcid">telephoneField</span>" type="<A href="ValidField.html">ValidField</a>"&gt;
&lt;binding name="value" expression="lotBid.bidderTelephone"/&gt;
&lt;binding name="validator" expression="beans.telephoneValidator"/&gt;
&lt;static-binding name="displayName" value="Telephone"/&gt;;
&lt;/component&gt;
public abstract class LotBidPage extends BasePage {
public abstract LotBid getLotBid();
public abstract void setLotBid(LotBid value);
public asbtract AuctionDetails getAuctionDetails();
public abstract void setAuctionDetails(AuctionDetails value);
public void okSubmit(IRequestCycle cycle) {
ValidationDelegate delegate = (ValidationDelegate)
getBeans().getBean("delegate");
// If no errors process the bid, otherwise stay on this page and
// let the fields show their errors.
if (!delegate.getHasErrors())
// Save the lot bid to the database.
..
// Go to the confirmation page.
cycle.activate("BidConfirmPage");
}
}
public void cancelSubmit(IRequestCycle cycle) {
cycle.activate("AuctionListPage");
}
}
public class LotBid implements Serializable {
private int lotNo;
private float bidAmount;
private String bidderName;
private String bidderEmail;
private String bidderTelephone;
public int getLotNo() { return lotNo; }
public void setLotNo(int value) {
lotNo = value;
}
public float getBidAmount() { return bidAmount; }
public void setBidAmount(float value) {
bidAmount = value;
}
public float getBidderName() { return bidderName; }
public void setBidderName(String value) {
bidderName = value;
}
public float getBidderEmail() { return bidderEmail; }
public void setBidderEmail(String value) {
bidderEmail = value;
}
public float getBidderTelephone() { return bidderTelephone; }
public void setBidderTelephone(String value) {
bidderTelephone = value;
}
}
public class AuctionDetails implements Serializable {
private int numberLots;
private float minBid;
private float maxBid;
public AuctionDetails(int numberLots, float minBid, float maxBid) {
this.numberLots = numberLots;
this.minBid = minBid;
this.maxBid = maxBid;
}
public int getNumberLots() { return numberLots; }
public float getMinBid() { return minBid; }
public float getMaxBid() { return maxBid; }
}
</pre>
</td>
</tr></table>
</td></tr>
<tr>
<td colspan="2"><hr></td>
</tr>
<tr>
<td align="left"><A href="Upload.html"><IMG alt=Upload src="common-images/prev.png"></a></td>
<td align="right"><A href="index.html"><IMG alt="Component Index" src="common-images/home.png" ></a></td>
</tr>
</table>
</body>
</html>