This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Other Application Frameworks and the fly likes Spring Commons Validation with AbstractWizardFormController Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Spring Commons Validation with AbstractWizardFormController" Watch "Spring Commons Validation with AbstractWizardFormController" New topic
Author

Spring Commons Validation with AbstractWizardFormController

Murali Pen
Greenhorn

Joined: May 23, 2006
Posts: 28
I couldn't get this to work after following every instruction in the spring modules online documentation. When I click the next button on my registration page (with some required fields left empty), I simply see the next page in the order. No validation is happening. Can someone help?

Here is my code and configuration.

Controller code:
===============
protected void validatePage(Object command, Errors errors, int page) {

User user = (User)command;
Validator validator = (DefaultBeanValidator)getValidator();

if(page == 0){
validator.validate(user, errors);
}

}

registerForm.jsp:
================
<form:form action="register.htm" method="post" commandName="user">
<table>
<tr>
<td>User Name*</td>
<td>
<form:input path="userName" />
<form:errors path="userName" cssClass="error"/>
</td>
</tr>
<tr>
<td>Password*</td>
<td>
<form assword path="password" />
<form:errors path="password" cssClass="error"/>
</td>
</tr>
<tr>
<td>Re-enter password*</td>
<td>
<form assword path="confirmPassword" />
<form:errors path="confirmPassword" cssClass="error"/>
</td>
</tr>
<tr>
<td>First Name*</td>
<td>
<form:input path="firstName" />
<form:errors path="firstName" cssClass="error"/>
</td>
</tr>
<tr>
<td>Middle Initial</td>
<td><form:input path="middleInitial" /></td>
</tr>
<tr>
<td>Last Name*</td>
<td>
<form:input path="lastName" />
<form:errors path="lastName" cssClass="error"/>
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" name="_target1" value="Next" />
<input type="submit" name="_cancel" value="Cancel" />
</td>
</tr>
</table>
</form:form>

Spring config:
=============

<bean id="validatorFactory" class="org.springmodules.validation.commons.DefaultValidatorFactory">
<property name="validationConfigLocations">
<list>
<value>WEB-INF/validator-rules.xml</value>
<value>WEB-INF/validation.xml</value>
</list>
</property>
</bean>

<bean id="beanValidator" class="org.springmodules.validation.commons.DefaultBeanValidator">
<property name="validatorFactory" ref="validatorFactory" />
</bean>

<bean name="/register.htm" class="com.estore.controller.RegistrationController">
<property name="pages">
<list>
<value>registerForm</value>
<value>addressInfo</value>
<value>paymentInfo</value>
<value>registerSuccess</value>
<value>redirect:home.htm</value>
</list>
</property>
<property name="userService" ref="userService" />
<property name="lookupService" ref="lookupService" />
<property name="validator" ref="beanValidator" />
<property name="commandName" value="user"/>
<property name="commandClass" value="com.estore.command.User"/>
</bean>

validaton.xml
=============
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD
Commons Validator Rules Configuration 1.1//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_1.dtd">

<form-validation>
<formset>
<form name="user">
<field property="userName" depends="required">
<arg0 key="required.username" />
</field>
<field property="password" depends="required">
<arg0 key="required.password" />
</field>
<field property="confirmPassword" depends="required">
<arg0 key="required.confirmpassword" />
</field>
<field property="firstName" depends="required">
<arg0 key="required.firstname" />
</field>
<field property="lastName" depends="required">
<arg0 key="required.lastname" />
</field>
</form>
</formset>
</form-validation>

validator-rules.xml
===================
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_0.dtd">


<form-validation>

<global>

<validator name="required"
classname="org.springframework.validation.commons.FieldChecks"
method="validateRequired"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.springframework.validation.Errors"
msg="errors.required">

<javascript><![CDATA[....
]]>
</javascript>

</validator>

<validator name="requiredif"
classname="org.springframework.validation.commons.FieldChecks"
method="validateRequiredIf"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.springframework.validation.Errors,
org.apache.commons.validator.Validator"

msg="errors.required">
</validator>

</global>

</form-validation>


Murali
SCJP,SCBCD,SCWCD
Elie Hirschman
Ranch Hand

Joined: Dec 15, 2005
Posts: 37
Murali Pen wrote:I couldn't get this to work after following every instruction in the spring modules online documentation. When I click the next button on my registration page (with some required fields left empty), I simply see the next page in the order. No validation is happening. Can someone help?


Thought I'd answer this really old question, since I have the answer:

From the JavaDocs of the AbstractWizardFormController:
The Validator's default validate method
will not be called by a wizard form controller


Instead, you override WizardController's method validatePage(Object, org.springframework.validation.Errors , int).

I then call a specific validation method based on the current page number. Like so:
 
Don't get me started about those stupid light bulbs.
 
subject: Spring Commons Validation with AbstractWizardFormController
 
Similar Threads
Error messages are not being displayed while using validator
Struts Validation Empty alert Box
Problem with validation example
problem while loading the welcome page
what's wrong wtih this validation code