Upload Component Index

ValidField
org.apache.tapestry.valid.ValidField
Age
 
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
  • PatternValidator - validates text for a specified pattern
  • 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="org.apache.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="@Submit" listener="ognl:listeners.okSubmit" type="Submit" value="  OK  "/>
  <input jwcid="@Submit" listener="ognl:listeners.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.
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.

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.

Regal Auctions Bid Page

Bid Amount must be greater than 10.0
Lot Number
Bid Amount
Full Name
Email
Telphone

<form jwcid="@Form" delegate="ognl:beans.delegate">
<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" delegate="ognl:beans.delegate"/></td>
</tr>
<tr>
<td><span jwcid="@FieldLabel" field="ognl:components.lotNoField">Lot Number</span></td><td><input jwcid="lotNoField" type="text" size="4" maxlength="4"/></td>
</tr>
<tr>
<td><span jwcid="@FieldLabel" field="ognl:components.bidAmountField">Bid Amount</span></td><td><input jwcid="bidAmountField" type="text" size="7" maxlength="7"/></td>
</tr>
<tr>
<td><span jwcid="@FieldLabel" field="ognl:components.fullNameField">Full Name</span></td><td><input jwcid="fullNameField" type="text" size="25" maxlength="30"/></td>
</tr>
<tr>
<td><span jwcid="@FieldLabel" field="ognl:components.emailField">Email</span></td><td><input jwcid="emailField" type="text" size="25" maxlength="30"/></td>
</tr>
<tr>
<td><span jwcid="@FieldLabel" field="ognl:components.telephoneField">Telephone</span></td><td><input jwcid="telephoneField" type="text" size="25" maxlength="30"/></td>
</tr>
<tr>
<td colspan="2" align="right">
<input jwcid="@Submit" listener="ognl:listeners.okSubmit" type="Submit" value=" OK "/>
<input jwcid="@Submit" listener="ognl:listeners.cancelSubmit"onclick="form.onsubmit = null;"/>
</td>
</tr>
</table>
</form>
<property-specification name="lotBid" type="com.mycorp.LotBid" persistent="yes" initial-value="new com.mycorp.LotBid()"/>
<property-specification name="actionDetails" type="com.mycorp.ActionDetails" persistent="yes"/>
<bean name="delegate" class="com.mycorp.FormValidationDelegate"/> <bean name="lotNoValidator" class="org.apache.tapestry.valid.NumberValidator"> <set-property name="required" expression="true"/> <set-property name="minimum" expression="1"/> <set-property name="maximum" expression="auctionDetails.numberLots"/> <set-property name="valueType">"int"</set-property> </bean> <bean name="bidAmountValidator" class="org.apache.tapestry.valid.NumberValidator"> <set-property name="required" expression="true"/> <set-property name="minimum" expression="auctionDetails.minBid"/> <set-property name="maximum" expression="auctionDetails.maxBid"/> <set-property name="valueType">"float"</set-property> </bean> <bean name="fullNameValidator" class="org.apache.tapestry.valid.StringValidator"> <set-property name="required" expression="true"/> <set-property name="minimumLength" expression="3"/> </bean> <bean name="emailValidator" class="org.apache.tapestry.valid.StringValidator"> <set-property name="required" expression="true"/> <set-property name="minimumLength" expression="12"/> </bean> <bean name="telephoneValidator" class="org.apache.tapestry.valid.StringValidator"> <set-property name="required" expression="true"/> <set-property name="minimumLength" expression="11"/> </bean> <component id="lotNoField" type="ValidField"> <binding name="value" expression="lotBid.lotNo"/> <binding name="validator" expression="beans.lotNoValidator"/> <static-binding name="displayName" value="Lot Number"/> </component> <component id="bidAmountField" type="ValidField"> <binding name="value" expression="lotBid.bidAmount"/> <binding name="validator" expression="beans.bidAmountNoValidator"/> <static-binding name="displayName" value="Bid Amount"/> </component> <component id="fullNameField" type="ValidField"> <binding name="value" expression="lotBid.bidderName"/> <binding name="validator" expression="beans.fullNameValidator"/> <static-binding name="displayName" value="Full Name"/> </component> <component id="emailField" type="ValidField"> <binding name="value" expression="lotBid.bidderEmail"/> <binding name="validator" expression="beans.emailValidator"/> <static-binding name="displayName" value="Email"/> </component> <component id="telephoneField" type="ValidField"> <binding name="value" expression="lotBid.bidderTelephone"/> <binding name="validator" expression="beans.telephoneValidator"/> <static-binding name="displayName" value="Telephone"/>; </component> 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; } }

Upload Component Index