I've sorted it out. No thanks to any of the documentation.
I've been testing it in the petClinic application, here's what I've done in order to get client side and server side validation working.
petclinic-servlet.xml:
add
Code:
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:order="0" />
and
Code:
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="order"><value>3</value></property>
<property name="interceptors">
<list>
<bean class="org.springmodules.validation.valang.javascript.taglib.ValangRulesExportInterceptor" />
</list>
</property>
</bean>
create an xml file for defining the validation stuff, and put this in it:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="ht tp :/ /ww w.springframework.org/schema/beans"
xmlns:xsi="ht tp : / /ww w.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="ht tp://ww w.springframework.org/schema/beans
ht tp :/ /ww w.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="ownerValidator" class="org.springmodules.validation.valang.ValangValidator">
<property name="valang">
<value>
<![CDATA[
{ firstName : length(?) < 30 : 'First name too long' }
{ lastName : length(?) < 50 : 'Last name too long' }
]]>
</value>
</property>
</bean>
</beans>
make sure spring picks this file up by referencing it in your web.xml:
Code:
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext-jdbc.xml
/WEB-INF/validation.xml
</param-value>
In the jsp page (if you're trying this out on petclinic - I've used ownerForm.jsp) make sure you reference the varlang tag library
Code:
<%(at) taglib uri="ht tp :/ / www .spring modules.org/tags/valang" prefix="valang" >
and plonk the codebase in the head of the page somewhere
Code:
<valang:codebase includeScriptTags="true" fieldErrorsIdSuffix="_error" globalErrorsId="global_error"/>
All the examples I've seen tell you that you reference the commandName of the form in the valang:validate tag. I've done this :
Code:
<form:form modelAttribute="owner" >
<valang:validate commandName="ownerValidator" />
and it works just fine.
In the controller (AddOwnerForm.java) we've got this
Code:
package org.springframework.samples.petclinic.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.Clinic;
import org.springframework.samples.petclinic.Owner;
import org.springframework.samples.petclinic.validation.OwnerValidator;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springmodules.validation.valang.ValangValidator;
/**
* JavaBean form controller that is used to add a new <code>Owner</code> to
* the system.
*
* author Juergen Hoeller
* author Ken Krebs
*/
(at)Controller
(at)RequestMapping("/addOwner.do")
(at)SessionAttributes(types = Owner.class)
public class AddOwnerForm extends SimpleFormController{
private Clinic clinic;
private ValangValidator ownerValidator;
(at)Autowired
public AddOwnerForm(Clinic clinic, ValangValidator ownerValidator) {
this.clinic = clinic;
this.ownerValidator = ownerValidator;
}
(at)RequestMapping(method = RequestMethod.GET)
public String setupForm(Model model) {
Owner owner = new Owner();
model.addAttribute(owner);
model.addAttribute("ValangRules.ownerValidator", ownerValidator.getRules());
return "ownerForm";
}
(at)RequestMapping(method = RequestMethod.POST)
public String processSubmit((at)ModelAttribute Owner owner, BindingResult result, SessionStatus status) {
ownerValidator.validate(owner, result);
if (result.hasErrors()) {
return "ownerForm";
}
else {
this.clinic.storeOwner(owner);
status.setComplete();
return "redirect:owner.do?ownerId=" + owner.getId();
}
}
}
And thats it. This should take care of the client side and the server side validation.
Hope this helps someone.