Description
The following is the step to create a Client-Side validation using webwork. Note the validate attribute is set to true. Note also that not all themes support this feature (client-side validation)
Step 1
Create the jsp page. Note the <ww:head > tag being used, it will setup the css in this case (xhtml theme)
<html>
<head>
<title>Validation - Basic</title>
<ww:head />
</head>
<body>
<p>
The following form uses labelposition="left"
<ww:form id="f1" name="quizClient" namespace="/validation" method="post" validate="true">
<ww:textfield label="Name" name="name" labelposition="left" />
<ww:textfield label="Age" name="age" labelposition="left" />
<ww:textfield label="Favorite color" name="answer" labelposition="left" />
<ww:submit/>
</ww:form>
</p>
<br/>
<p>
The following form uses labelposition="top"
<ww:form id="f2" name="quizClient" namespace="/validation" method="post" validate="true">
<ww:textfield label="Name" name="name" labelposition="top" />
<ww:textfield label="Age" name="age" labelposition="top" />
<ww:textfield label="Favorite color" name="answer" labelposition="top" />
<ww:submit/>
</ww:form>
</p>
</body>
</html>
Step 2
Create the action class
public class QuizAction extends ActionSupport {
int minAge = 13;
int maxAge = 19;
String name;
int age;
String answer;
public int getMinAge() {
return minAge;
}
public void setMinAge(int minAge) {
this.minAge = minAge;
}
public int getMaxAge() {
return maxAge;
}
public void setMaxAge(int maxAge) {
this.maxAge = maxAge;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
}
Step 3
Create the validation.xml to configure the validators to be used.
<!--
Add the following DOCTYPE declaration as first line of your XXX-validation.xml file:
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.3//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.3.dtd">
-->
<validators>
<field name="name">
<field-validator type="requiredstring">
<message key="validation.name.required" />
</field-validator>
</field>
<field name="age">
<field-validator type="int">
<param name="min">10</param>
<param name="max">19</param>
<message key="validation.age.invalid">
<param name="0">'Contestant'</param>
<param name="1">minAge</param>
<param name="2">maxAge</param>
<param name="defaultMessage">Your age, its invalid</param>
</message>
</field-validator>
</field>
</validators>
|