Hi,
I want to use *Commons
Validator<http://commons.apache.org/validator/index.html>
*in my web project. I have a servlet based web-app and I need to add
validations on the beans populated from request.
I created a validation.xml like this
<form-validation>
<global>
<validator name=*"required"*
classname=*"com.pbbi.faraday.common.validator.Validator"*
method=*"validateRequired"*
methodParams=*"java.lang.Object,
org.apache.commons.validator.Field"*/>
</global>
<formset>
<form name=*"unitLocationBean"*>
<field property=*"unitId"* depends=*"required"*>
<arg0 key=*"unitlocation.unitid"*/>
</field>
</form>
</formset>
</form-validation>
And my validator code is like this
import org.apache.commons.validator.Field;
import org.apache.commons.validator.GenericValidator;
import org.apache.commons.validator.util.ValidatorUtils;
public class Validator {
public static boolean validateRequired(Object bean, Field field) {
String value = ValidatorUtils.getValueAsString(bean,
field.getProperty());
return !GenericValidator.isBlankOrNull(value);
}
}
And my test class to test the validations is like this
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.commons.validator.Validator;
import org.apache.commons.validator.ValidatorResources;
import org.apache.commons.validator.ValidatorResult;
import org.apache.commons.validator.ValidatorResults;
import com.pbbi.faraday.beans.UnitLocationBean;
public class Test {
public void testname() throws Exception {
InputStream in = new FileInputStream(new
File("D:\\workspace\\faradaytc\\src\\main\\resources\\validator-rules.xml"));
// InputStream in1 = new FileInputStream(new
File("D:\\workspace\\faradaytc\\src\\main\\resources\\MessageResources.properties"));
InputStream[] ins=new InputStream[2];
ins[0]=in;
// ins[1]=in1;
// Create an instance of ValidatorResources to
// initialize from an xml file.
ValidatorResources resources = new ValidatorResources(in);
// Create bean to run test on.
UnitLocationBean unitLocationBean=new UnitLocationBean();
unitLocationBean.setUnitId("12121");
// Construct validator based on the loaded resources
// and the form key
Validator validator = new Validator(resources, "unitLocationBean");
// add the name bean to the validator as a resource
// for the validations to be performed on.
validator.setParameter(Validator.BEAN_PARAM, unitLocationBean);
// throws ValidatorException,
// but aren't catching for example
ValidatorResults results2=validator.validate();
ValidatorResult result=results2.getValidatorResult("unitId");
// result.getResult(validatorName)
System.out.println("results2 :
"+results2.getValidatorResult("unitId"));
System.out.println("results2 :
"+results2.getValidatorResult("unitId"));
if (results2.getValidatorResult("unitId") == null) {
System.out.println("No error :
"+results2.getValidatorResult("unitId"));
} else {
// number of errors for first name
//int errors = ((Integer)results.get("firstName")).intValue();
System.out.println(results2.getValidatorResult("unitId"));
System.out.println(" errors
:"+results2.getValidatorResult("unitId"));
}
}
public static void main(String[] args) {
try {
new Test().testname();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I am not able to understand where to put the message resource property file
to show up the messages.
I want server side validations only in this case.
Regards,
Gagan Suri,
--
Gagan Suri
"Going to Sleep does not solve the problems
you will have to face them the next day.."