firstName
and lastName
with JSR 303's @NotNull
and @Size
.
clientValidation
parameter.Bean Validation is not in Tapestry's corelib. To use it, add the following jars to the classpath:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- We need a doctype to allow us to use special characters like
We use a "strict" DTD to make IE follow the alignment rules. -->
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_4.xsd">
<body class="container">
<h3>Validators</h3>
Client-side and server-side, validation occurs AFTER translation.<br/><br/>
Tapestry has two declarative styles of validation: industry-standard
<a href="http://en.wikipedia.org/wiki/Bean_Validation">Bean Validation</a> (JSR 303), and Tapestry Validators.<br/><br/>
Here we demonstrate Bean Validation, using the page as a bean. We annotated the page's properties
<code>firstName</code> and <code>lastName</code> with JSR 303's <code>@NotNull</code> and <code>@Size</code>.
<div class="eg">
<t:form class="form-horizontal">
<div class="form-group">
<t:label for="firstName" class="col-sm-3"/>
<div class="col-sm-3">
<t:textfield t:id="firstName"/>
</div>
<div class="col-sm-6">
<p class="form-control-static">
${firstName}
<span class="text-muted">
(not null, size max = 10)
</span>
</p>
</div>
</div>
<div class="form-group">
<t:label for="lastName" class="col-sm-3"/>
<div class="col-sm-3">
<t:textfield t:id="lastName"/>
</div>
<div class="col-sm-6">
<p class="form-control-static">
${lastName}
<span class="text-muted">
(not null, size max = 10)
</span>
</p>
</div>
</div>
<div class="form-group">
<div class="col-sm-3 col-sm-offset-3">
<t:submit/>
</div>
</div>
</t:form>
</div>
All bean validations have a server-side implementation, and most (or all?) have a client-side implementation.<br/>
If the form passes client-side validation, then it is sent to the server, where it is validated again.<br/>
To turn off client-side validation programatically, set Form's <code>clientValidation</code> parameter.<br/><br/>
<p>Bean Validation is not in Tapestry's corelib. To use it, add the following jars to the classpath:</p>
<ul>
<li>tapestry-beanvalidator-5.4.n.jar, for compile and runtime.</li>
<li>a JSR 303 implementation, such as hibernate-validator-4.3.1.Final.jar, for runtime.
It can be provided by the runtime environment.</li>
</ul>
and follow the Configuration information <a href="http://tapestry.apache.org/bean-validation.html">here</a>.<br/><br/>
References:
<a href="http://tapestry.apache.org/bean-validation.html">Bean Validation</a>,
<a href="http://docs.oracle.com/javaee/6/api/javax/validation/constraints/package-summary.html">the standard annotations</a>,
<a href="http://tapestry.apache.org/5.4/apidocs/org/apache/tapestry5/corelib/components/Form.html">Form</a>,
<a href="http://tapestry.apache.org/5.4/apidocs/org/apache/tapestry5/corelib/components/TextField.html">TextField</a>.
<br/><br/>
<t:pagelink page="Index">Home</t:pagelink><br/><br/>
<t:tabgroup>
<t:sourcecodetab src="/web/src/main/java/jumpstart/web/pages/examples/input/Validators.tml"/>
<t:sourcecodetab src="/web/src/main/java/jumpstart/web/pages/examples/input/Validators.java"/>
<t:sourcecodetab src="/web/src/main/resources/META-INF/assets/css/examples/plain.css"/>
</t:tabgroup>
</body>
</html>
package jumpstart.web.pages.examples.input;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.apache.tapestry5.PersistenceConstants;
import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
@Import(stylesheet = "css/examples/plain.css")
public class Validators {
// Screen fields
@Property
@Persist(PersistenceConstants.FLASH)
@NotNull
@Size(max = 10)
private String firstName;
@Property
@Persist(PersistenceConstants.FLASH)
@NotNull
@Size(max = 10)
private String lastName;
}
.eg {
margin: 20px 0;
padding: 14px;
border: 1px solid #ddd;
border-radius: 6px;
-webkit-border-radius: 6px;
-mox-border-radius: 6px;
}