onValidateFromInputs()
.
Yes, we could have avoided it by using Bean Validation's @Pattern
annotation instead.
<!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>Programmatic Validation</h3>
Here we demonstrate programmatic validation in the event handler <code>onValidateFromInputs()</code>.
Yes, we could have avoided it by using Bean Validation's <code>@Pattern</code> annotation instead.
<div class="eg">
<t:form class="form-horizontal" t:id="inputs">
<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">
(required, maxLength=10, letters only)
</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">
(required, maxLength=10, letters only)
</span>
</p>
</div>
</div>
<div class="form-group">
<div class="col-sm-3 col-sm-offset-3">
<t:submit/>
</div>
</div>
</t:form>
</div>
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/Pattern.html">@Pattern API</a>,
<a href="http://tapestry.apache.org/forms-and-validation.html">Forms and Validation</a>,
<a href="http://tapestry.apache.org/5.4/apidocs/org/apache/tapestry5/corelib/components/Form.html">Form</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/ProgrammaticValidation.tml"/>
<t:sourcecodetab src="/web/src/main/java/jumpstart/web/pages/examples/input/ProgrammaticValidation.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.InjectComponent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.corelib.components.TextField;
@Import(stylesheet = "css/examples/plain.css")
public class ProgrammaticValidation {
// Screen fields
@Property
@Persist(PersistenceConstants.FLASH)
@NotNull
@Size(max = 10)
private String firstName;
@Property
@Persist(PersistenceConstants.FLASH)
@NotNull
@Size(max = 10)
private String lastName;
// Generally useful bits and pieces
@InjectComponent("inputs")
private Form form;
@InjectComponent("firstName")
private TextField firstNameField;
@InjectComponent("lastName")
private TextField lastNameField;
// The code
void onValidateFromInputs() {
// Error if the names don't contain letters only
if (firstName != null) {
if (!firstName.matches("[A-Za-z]+")) {
form.recordError(firstNameField, "First Name must contain letters only");
}
}
if (lastName != null) {
if (!lastName.matches("[A-Za-z]+")) {
form.recordError(lastNameField, "Last Name must contain letters only");
}
}
}
}
.eg {
margin: 20px 0;
padding: 14px;
border: 1px solid #ddd;
border-radius: 6px;
-webkit-border-radius: 6px;
-mox-border-radius: 6px;
}