• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Spring Commons Validation with AbstractWizardFormController

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
 
And will you succeed? Yes you will indeed! (98 and 3/4 % guaranteed) - Seuss. tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic