java ee - Bean Validation Message with dynamic parameter -


i getting started bean validation, , i'm trying compose constraint. constraint validate cpf(personal document in brazil). constraint working, need message contain dynamic parameter.

i'm using validationmessages.properties. code:

@constraint(validatedby=cpfvalidator.class) @size(min=11, max=14) @documented @target({elementtype.field,elementtype.method}) @retention(retentionpolicy.runtime) public @interface cpf {      string message() default "{cpf.validation.message}";     class<?>[] groups() default {};     class<? extends payload>[] payload() default {};  } 

my validationmessages.properties:

cpf.validation.message=cpf {cpf} é inválido 

my validator: i'm using context.buildconstraintviolationwithtemplate customiza message.

@override public boolean isvalid(string value, constraintvalidatorcontext context) {      string cpf = value;      boolean result = validationutil.validacpf(cpf);     if (result) {         return true;     }      context.disabledefaultconstraintviolation();     context.buildconstraintviolationwithtemplate("pf.validation.message}")            .addconstraintviolation();     return false;  } 

how can pass validated value(cpf) parameter when message created?

when working hibernate validator >= 4.2 can reference validated value via ${validatedvalue} in messages (in bean validation 1.1 that's standardized in spec):

cpf.validation.message=cpf ${validatedvalue} é inválido 

btw. hibernate validator comes @cpf constraint, can find out more in reference guide. glad hear how works you.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -