Jakarta > Tapestry
Jakarta
 

ValidField

An extended version of TextField that renders an <input> form element but allows for pluggable validation, both on the server (at form submission time) and actively on the client.

See also: org.apache.tapestry.valid.ValidField, FieldLabel, Form, TextField

Warning
This component has been deprecated; Tapestry 4.0 adds even better support for validation to TextField, TextArea and most other form element component.

Parameters

Name Type Direction Required Default Description
value java.lang.Object in / out yes   The value to be edited which may be any reasonable type. The validator is responsible for converting back and forth between server-side data types (such as int, long, Date, BigDecimal, etc.) and client-side representation (as a simple string).
validator IValidator int yes   The validator object for this field. Validators are stateless and, therefore, highly sharable. The validator binding allows validator type and configuration to be specified succinctly; where more control is necessary, validators will often be defined as managed beans.
disabled boolean in no false If true, then a disabled attribute will be rendered as part of the <input> tag, and the component will not update its value parameter when the form is submitted.
displayName string in no   The user-presentable name for the component, which will be used by a FieldLabel connected to the component. This name is also used by the validator, when creating client- or server-side error messages.
hidden boolean in no false If true, then the type attribute will be "password", not "text", and user input in the browser will be masked.

Body: removed

Informal parameters: allowed

Reserved parameters: name, type, value

Example

This example shows the usage of a number of ValidField components, each configured differently.

EnterBid.html

<form jwcid="form@Form" delegate="bean:delegate">
  <table>
    <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="beans.delegate"/></td>
    </tr>
    <tr>
      <td><span jwcid="@FieldLabel" field="component:lotNoField">Lot Number</span></td>
      <td><input jwcid="lotNoField" type="text" size="4" maxlength="4"/></td> 
    </tr>
    <tr>
      <td><span jwcid="@FieldLabel" field="component:bidAmountField">Bid Amount</span></td>
      <td><input jwcid="bidAmountField" type="text" size="7" maxlength="7"/></td>
    </tr>
    <tr>
      <td><span jwcid="@FieldLabel" field="component:fullNameField">Full Name</span></td>
      <td><input jwcid="fullNameField" type="text" size="25" maxlength="30"/></td>
    </tr>
    <tr>
      <td><span jwcid="@FieldLabel" field="component:emailField">Email</span></td>
      <td><input jwcid="emailField" type="text" size="25" maxlength="30"/></td>
    </tr>
    <tr>
      <td><span jwcid="@FieldLabel" field="component: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="listener:doSubmit" type="Submit" value="   OK   "/>
       <input jwcid="@Submit" listener="listener:doCancel" onclick="javascript:form.events.cancel();"/>
     </td>
   </tr>
  </table>
</form>

Because of the number of parameters for ValidField, they are traditionally defined in the specification.

EnterBid.page (partial):


<bean name="delegate" class="org.apache.tapestry.valid.ValidationDelegate" property="delegate"/>

<bean name="lotNoValidator" class="org.apache.tapestry.valid.NumberValidator">
    <set name="required" value="true"/>
    <set name="minimum" value="1"/>
    <set name="maximum" value="auctionDetails.numberLots"/>
    <set name="valueType">"int"</set>
</bean>

<bean name="bidAmountValidator" class="org.apache.tapestry.valid.NumberValidator">
    <set name="required" value="true"/>
    <set name="minimum" value="auctionDetails.minBid"/>
    <set name="maximum" value="auctionDetails.maxBid"/>
    <set name="valueType">"float"</set>    
</bean>

<component id="lotNoField" type="ValidField">
    <binding name="value" value="lotBid.lotNo"/>
    <binding name="validator" value="bean:lotNoValidator"/>
    <binding name="displayName" value="literal:Lot Number"/>
</component>

<component id="bidAmountField" type="ValidField">
    <binding name="value" value="lotBid.bidAmount"/>
    <value name="validator" value="bean:bidAmountNoValidator"/>
    <value name="displayName" value="literal:Bid Amount"/>
</component>

<component id="fullNameField" type="ValidField">
    <binding name="value" value="lotBid.bidderName"/>
    <binding name="validator" value="validator:string,required,minimumLength=3"/>
    <binding name="displayName" value="literal:Full Name"/>
</component>

<component id="emailField" type="ValidField">
    <binding name="value" value="lotBid.bidderEmail"/>
    <binding name="validator" value="validator:string,required,minimumLength=12"/>
    <binding name="displayName" value="literal:Email"/>
</component>

<component id="telephoneField" type="ValidField">
    <binding name="value" value="lotBid.bidderTelephone"/>
    <binding name="validator" value="validator:string,required,minimumLength=11"/>
    <binding name="displayName" value="literal:Telephone"/>;
</component>

EnterBid.java:

public abstract class EnterBid extends BasePage {
    public abstract LotBid getLotBid();

    public abstract void setLotBid(LotBid value);
    public asbtract AuctionDetails getAuctionDetails();

    public abstract void setAuctionDetails(AuctionDetails value);
    
    public abstract IValidationDelegate getDelegate();

    public String doSubmit() {
        IValidationDelegate delegate = getDelegate();

        // If no errors process the bid, otherwise stay on this page and 
        // let the fields show their errors.
        if (delegate.getHasErrors())
          return null;
          
        // Save the lot bid to the database.
        // ..
            
        return "BidConfirmPage";
    }
    
    public String doCancel() {
       return "AuctionListPage";
    }
}