java - Custom error messaging on Hibernate Validation -


i trying set hibernate validation in ecommerce site. have order object multiple objects assigned. customer goes through checkout, want able individually validate these objects - multiple objects 1 form.

for example, upon submitting delivery form, deliverycharge , deliveryaddress should validated. if validation fails, delivery form returned list of validation errors.

i can validate objects via java implementation, when try view these on view tier using <form:error /> tag, not getting anything.

order model

@entity @table(name = "bees_address") public class address {   @onetoone   @joincolumn(name = "paymentaddress")   private address payment;    @onetoone   @joincolumn(name = "deliveryaddress")   private address payment;    @column(name = "deliverycharge")   private integer deliverycharge;   ... 

address model

@entity @table(name = "bees_address") public class address {   @size(min=2, max=150)   @column(name = "line1", length = 150)   private string line1;   ... 

controller

public string updatedelivery(httpservletrequest request, @modelattribute("basket") order basketupdate) {    address deliveryaddress = basketupdate.getdeliveryaddress();    if (!validate.isvalid(request, deliveryaddress)) {     logger.info("delivery address not validate");     return "redirect:/checkout/delivery";              } else {     /* stuff here */   }    return "redirect:/checkout/payment"; } 

validation hibernate validation docs

public static boolean isvalid(httpservletrequest request, address address) {   validatorfactory factory = validation.builddefaultvalidatorfactory();   validator  validator = factory.getvalidator();    set<constraintviolation<address>> constraintviolations = validator.validate(address);   request.getsession().setattribute("formerrors", constraintviolations);    return constraintviolations.size() < 1; } 

jsp structure

<form:form action="${url}" method="post" modelattribute="basket"   charge: <form:input path="deliverycharge" />   address: <form:input path="deliveryaddress.line1" />            <form:error path="deliveryaddress.line1" />   ... 

many thanks

i think after validation groups. allow validate different sets of constraints in several steps.

declare required groups defining interfaces , assign constraints 1 or more groups:

public interface deliverychecks{} public interface paymentchecks{}  public class address {      @notnull(groups = paymentchecks.class)     private address payment;      @min(value=5, groups = deliverychecks.class)     private integer deliverycharge;     ... } 

then validate required group(s) each form or page passing group identifiers validate():

 set<constraintviolation<address>> constraintviolations =      validator.validate(address, paymentchecks.class); 

the hibernate validator reference guide has more information on topic.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -