we using spring mvc 3.0 in our web application. using validation framework of spring mvc.
while doing validation need create our validators each entity need validate. example if have person entity, validate using following personvalidator.
public class personvalidator implements validator { /** * validator validates person instances */ public boolean supports(class clazz) { return person.class.equals(clazz); } public void validate(object obj, errors e) { validationutils.rejectifemptyorwhitespace(errors, "firstname", "field.required"); validationutils.rejectifemptyorwhitespace(errors, "surname", "field.required"); validationutils.rejectifemptyorwhitespace(errors, "age", "field.required"); } }
my question is, possible have different validations same entities different methods.
@requestmapping(method = requestmethod.post) public string add(@valid person person, bindingresult result) { if (result.haserrors()) { return "person/new"; } personservice.addperson(person); return "redirect:/persons"; } @requestmapping(method = requestmethod.post) public string update(@valid person person, bindingresult result) { if (result.haserrors()) { return "person/edit"; } personservice.updateperson(person); return "redirect:/persons"; }
i want validate first name, last name , age while adding new person while updating don't want age mandatory.
this random situation, can entity , property.
how handle such situations?
thanks.
you drop @valid
annotation , instead inside method set flag on model object insert vs update , call validator directly (you can inject them controller).
then inside validator can choose validations required current scenario.
Comments
Post a Comment