-
Oct 28th, 2005, 07:31 AM
#1
validation with multiple objects
I am looking at how Spring handles validation and I am curious how I can validate a form that using multiple objects. I will give you an example from a tutorial found in the Spring Developer's Notebook. It seems to me that the validator is tied to a specific object in the case below com.springbook.Bike. What if my form interacts with multiple objects and the attributies of the form are not in one object but many. Am I missing it?
the actual validateor code
package com.springbook;
import org.springframework.validation.Validator;
import org.springframework.validation.Errors;
public class BikeValidator implements Validator {
public boolean supports(Class aClass) {
return aClass.equals(Bike.class);
}
public void validate(Object o, Errors errors) {
Bike bike = (Bike)o;
if(bike == null) {
errors.rejectValue("manufacturer", "error.not-specified", null, "Value required.");
} else {
System.out.println("VALIDATOR: bike.manufacturer=" + bike.getManufacturer());
if(bike.getManufacturer() == null || "".equals(bike.getManufacturer()))
errors.rejectValue("manufacturer", "Value not present.", null, "Manufacturer required.");
if(bike.getModel() == null || "".equals(bike.getModel()))
errors.rejectValue("model", "Value not present.", null, "Model is required.");
}
}
}
code in the context xml to add teh validator to a form
<bean id="bikeValidator" class="com.springbook.BikeValidator"/>
<bean id="editBikeForm" class="com.springbook.SubmitBikeController">
<property name="sessionForm"><value>true</value></property>
<property name="commandName"><value>bike</value></property>
<property name="commandClass"><value>com.springbook.Bike</value></property>
<property name="validator"><ref bean="bikeValidator"/></property>
<property name="formView"><value>editBike</value></property>
<property name="successView"><value>bikes.bikes</value></property>
<property name="facade"><ref bean="rentaBike"/></property>
</bean>
-
Oct 28th, 2005, 09:11 AM
#2
You can wrap your multiple objects in one form object corresponding to your page. This one object can contain references to all of your other objects and expose them as bean properties.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules