|
|
|
Description
Provides an extended TextField that performs
automated validation and conversion of user entered text into typed Objects.
The ValidField component is rendered as a HTML <input type="text"> field
and must be wrapped by a Form component.
Each ValidField is configured with a
validator which
checks the correctness of the user's input and performs the input text to
java Object translations. The Form tracks the state of
all its ValidFields using a validation
delegate.
Using the validation delegate you can determine whether all the form's fields
are valid and inform the user of any validation errors.
There are number of pluggable validators you can assign to a ValidField
including:
-
DateValidator
- validates date strings and allows a minimum and maximum date range to be set
-
EmailValidator
- validates email strings and allows a minimum and maximum length to be set
-
NumberValidator
- validates number strings and allows a minimum and maximum values to be set
-
StringValidator
- validates string values and allows a minimum length to be set
The ValidField's
BaseValidator
classes automatically provide powerful client-side JavaScript field validation
when their parent Form 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:
<bean name="emailValidator" class="net.sf.tapestry.valid.EmailValidator">
<set-property name="clientScriptingEnabled" expression="true"/>
<set-property name="minimumLength" expression="8"/>
<set-property name="required" expression="true"/>
</bean>
Note if a form includes a "Cancel" style submit button, the cancel button will
need to set the form's onsubmit event handler to null, so that the
JavaScript field validation is not invoked when the form is submitted.
For example:
<input jwcid="okSubmit" type="Submit" value=" OK "/>
<input jwcid="cancelSubmit" type="Submit" value="Cancel" onclick="form.onsubmit = null;"/>
|
See Also
FieldLabel,
Form,
TextArea,
TextField
|
Parameters
Name |
Type |
Direction |
Required |
Default |
Description |
value |
Object |
in-out |
yes |
|
The value to be displayed (on render), and updated (when the form is
submitted, if the submitted value is valid). The
IValidator
converts between object values and Strings.
|
validator |
IValidator
|
in |
yes |
|
Object used to convert object values to Strings (for renderring)
and to validate and convert Strings into object values (when the form
is submitted).
|
displayName |
String |
in |
yes |
|
A textual name for the field that is used when formulating error messages.
Also used by the FieldLabel component to
properly label the field.
|
hidden |
boolean |
in |
no |
false |
If true, then the text field is written as a <input type="password">
form element.
|
type |
String |
in |
no |
Object |
The name of a class, to convert input into.
Required by some validators to identify the type of conversion from
String (typically, validators that convert a numeric amount).
If the class is in package java.lang, then the package name can be ommitted.
Also, the names of Java scalar types are acceptible.
|
hidden |
boolean |
in |
no |
false |
If true, then the text field is written as a <input type="password">
form element.
|
disabled |
boolean |
in |
no |
false |
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.
|
displayWidth |
integer |
in |
no |
|
Controls the display width of the text control in the client browser. If
unspecified or zero, then the width is left to the client browser to
determine. Corresponds to the "size" HTML attribute.
|
maximumLength |
integer |
in |
no |
|
Controls the maximum characters that the text control will accept. If
unspecified or zero, then the value is left to the client browser to
determine. Corresponds to the "maxlength" HTML attribute.
|
Body: removed
Informal parameters:allowed
Reserved parameters: "name", "type", "value"
|
Examples
The ValidField is used in this example to provide an auction bidding page. A
custom
ValidationDelegate
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.
For a more extensive example see the
Tapestry Workbench. The Fields
example in the Workbench also illustrates how to use the
FieldLabel component to high light invalid
fields.
<form jwcid="form">
<table class="examples" cellpadding="2">
<tr>
<td colspan="2"><span class="title">Regal Auctions Bid Page</span></td>
</tr>
<tr>
<td colspan="2"><hr></td>
</tr>
<tr>
<td colspan="2"><span jwcid="showError"/></td>
</tr>
<tr>
<td>Lot Number</td><td><input jwcid="lotNoField" type="text"/></td>
</tr>
<tr>
<td>Bid Amount</td><td><input jwcid="bidAmountField" type="text"/></td>
</tr>
<tr>
<td<Full Name</td><td><input jwcid="fullNameField" type="text"/></td>
</tr>
<tr>
<td>Email</td><td><input jwcid="emailField" type="text"/></td>
</tr>
<tr>
<td>Telphone</td><td><input jwcid="telephoneField" type="text"/></td>
</tr>
<tr>
<td colspan="2" align="right">
<input jwcid="okSubmit" type="Submit" value=" OK "/>
<input jwcid="cancelSubmit" type="Submit" value="Cancel" onclick="form.onsubmit = null;"/>
</td>
</tr>
</table>
</form>
<bean name="delegate" class="com.mycorp.FormValidationDelegate"/>
<bean name="lotNoValidator" class="net.sf.tapestry.valid.NumberValidator">
<set-property name="required" expression="true"/>
<set-property name="minimum" expression="1"/>
<set-property name="maximum" expression="auctionDetails.numberLots"/>
</bean>
<bean name="bidAmountValidator" class="net.sf.tapestry.valid.NumberValidator">
<set-property name="required" expression="true"/>
<set-property name="minimum" expression="auctionDetails.minBid"/>
<set-property name="maximum" expression="auctionDetails.maxBid"/>
</bean>
<bean name="fullNameValidator" class="net.sf.tapestry.valid.StringValidator">
<set-property name="required" expression="true"/>
<set-property name="minimum" expression="3"/>
</bean>
<bean name="emailValidator" class="net.sf.tapestry.valid.StringValidator">
<set-property name="required" expression="true"/>
<set-property name="minimum" expression="12"/>
</bean>
<bean name="telephoneValidator" class="net.sf.tapestry.valid.StringValidator">
<set-property name="required" expression="true"/>
<set-property name="minimum" expression="11"/>
</bean>
<component id="form" type="Form">
<binding name="delegate" expression="beans.delegate"/>
</component>
<component id="showError" type="ShowError">
<binding name="delegate" expression="beans.delegate"/>
</component>
<component id="lotNoField" type="ValidField">
<binding name="value" expression="lotBid.lotNo"/>
<binding name="validator" expression="beans.lotNoValidator"/>
<binding name="displayName" expression='"Lot Number"'/>
<binding name="displayWidth" expression="4"/>
<binding name="maximumLength" expression="4"/>
</component>
<component id="bidAmountField" type="ValidField">
<binding name="value" expression="lotBid.bidAmount"/>
<binding name="validator" expression="beans.bidAmountNoValidator"/>
<binding name="displayName" expression='"Bid Amount"'/>
<binding name="displayWidth" expression="7"/>
<binding name="maximumLength" expression="7"/>
</component>
<component id="fullNameField" type="ValidField">
<binding name="value" expression="lotBid.bidderName"/>
<binding name="validator" expression="beans.fullNameValidator"/>
<binding name="displayName" expression='"Full Name"'/>
<binding name="displayWidth" expression="25"/>
<binding name="maximumLength" expression="30"/>
</component>
<component id="emailField" type="ValidField">
<binding name="value" expression="lotBid.bidderEmail"/>
<binding name="validator" expression="beans.emailValidator"/>
<binding name="displayName" expression='"Email"'/>
<binding name="displayWidth" expression="25"/>
<binding name="maximumLength" expression="30"/>
</component>
<component id="telephoneField" type="ValidField">
<binding name="value" expression="lotBid.bidderTelephone"/>
<binding name="validator" expression="beans.telephoneValidator"/>
<binding name="displayName" expression='"Telephone"'/>
<binding name="displayWidth" expression="25"/>
<binding name="maximumLength" expression="30"/>
</component>
<component id="okSubmit" type="Submit">
<binding name="listener" expression="listeners.okSubmit"/>
</component>
<component id="cancelSubmit" type="Submit">
<binding name="listener" expression="listeners.cancelSubmit"/>
</component>
public class LotBidPage extends BasePage {
private LotBid lotBid = new LotBid();
private ActionDetails actionDetails;
public LotBid getLotBid() { return lotBid; }
public void setLotBid(LotBid value) {
lotBid = value;
fireObservedChange("lotBid", value);
}
public AuctionDetails getAuctionDetails() { return auctionDetails; }
public void setAuctionDetails(AuctionDetails value) {
auctionDetails = value;
fireObservedChange("auctionDetails", value);
}
public void detach() {
lotBid = new LotBid();
actionDetails = null;
super.detach();
}
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.setPage("BidConfirmPage");
}
}
public void cancelSubmit(IRequestCycle cycle) {
cycle.setPage("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; }
}
|
|