java - How to create custom Validation Messages based on an annotation property? -


i'm using hibernate @notnull validator, , i'm trying create custom messages tell user field has generated error when null. this:

notnull.custom = field {0} can't null. 

(this in validationmessages.properties file).

where {0} should field name passed validator way:

@notnull(field="field name") 

there way can that?

if requirement can satisfied interpolating hibernate messages, can create/name *property file that:

validationmessages.properties 

and inside that:

javax.validation.constraints.notnull.message = customized message when notnull violated! 

hibernate default searches resourcebundle named validationmessages. locale might involved: validationmessages_en, validationmessages_de, <..>

hibernate provide customized message through interpolatedmessage parameter, constraintviolationexception relevant information (included message ) showed. message part of real exception. unwieldy information provided!

if want make custom exception (without default constraintviolationexception behavior) check out:

using genericdao concept, consider following

  public void saveorupdate(ientity<?> entity) {       try {           if(entity.getid == null) {             em.persist(entity);           } else {             em.merge(entity)l           }       } catch(constraintviolationexception cve) {           throw new constraintviolationex(constructviolationmessage(cve.getconstraintviolations()));       }   }   private string constructmessage(set<constraintviolation<?>> pconstraintviolations) {     stringbuilder custommessages = new stringbuilder();     for(constraintviolation<?> violation : pconstraintviolations) {         string targetannotation = violation.getconstraintdescriptor().getannotation().annotationtype().getsimplename();         if(supportscustommessage(targetannotation)) {             applymessage(violation, targetannotation, custommessages);         } else {             // not customized constraints' messages e.g. append existing container         }     }     return custommessages.tostring();  }   private void applymessage(constraintviolation<?> pviolation, string ptargetannotation, stringbuilder pcustommessages) {      string targetclass = pviolation.getrootbean().getclass().getname();      string targetfield = pviolation.getpropertypath().tostring();      pcustommessages.append(messageformat.format(getmessagebyannotation(ptargetannotation), targetclass, targetfield));      pcustommessages.append(system.getproperty("line.separator"));  }    private string getbundlekey() {      return "validationmessages"; //fixme: hardcoded - implement true key  }   private string getmessagebyannotation(string ptargetannotation) {      resourcebundle messages = resourcebundle.getbundle(getbundlekey());      switch(ptargetannotation) {      case "notnull":          return messages.getstring(ptargetannotation + ".message");      default:          return "";      }  }   private boolean supportscustommessage(string ptargetannotation) {      return customizedconstraintstypes.contains(ptargetannotation);  } 

produced result:

test.model.exceptions.constraintviolationex     test.model.person : name cannot null     test.model.person : surname cannot null 

a hibernate constraintviolation provides relevant information root class , restricted field. see, applies hibernate supported constraints, need check if current annotation can customized supportscustommessage(<..>)! if can (it's you), should appropriate message constraint annotation doing `getmessagebyannotation(<..>)'.

all need implement not supported constraints logic. example can append it's cause message or interpolated default message (and true exception goes *log file)


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -