0

My scenario is :

I have a pojo class with JSR 303 validations as follows :

  public class Customer{

        String customerId;
        String name;
        BigDecimal salary;

        @NotNull(message = "CustomerId is mandatory")
        @Size(max = 7, message = "Invalid length for customerId")
        public void getCustomerId(){   
              return customerId;        
        }

         @NotNull(message = "Name is mandatory")
         public void getName(){   
              return name;        
        }

        @Digits(integer=5, fraction=3, message="Invalid length for salary")
        public void getSalary(){   
              return salary;        
        }

  }

My requirement is that I want to know which particular field failed the validation and I want the message written in the "message" attribute.

Currently, I am able to trace the fieldName but not the message that is present in the "message" attribute.

I am using the following method.

  public void validateCustomer(Customer customer){

       Errors errors = this.errorFactory.getInstance(Customer, 
       Customer.class.getName());
       jsrValidator.validate(customer, errors);
       if(errors.hasErrors()){
             if(errors.getFieldError() != null){
                   //fieldName that failed to validate.
                   String fieldName = errors.getFieldError().getField();

                   //I also want the message written in the attribute 
                     "message" of the annotation.

              }
       }
  }

I also want the message written in the attribute "message" of the annotation. How can I get it? Is it possible to do so?

Rabin Pantha
  • 941
  • 9
  • 19

1 Answers1

1

What is jsrValidator? I think it should be an instance of Validator, and have a validate(...) method which returns a Set. And the message should be available on each ConstraintViolation instance.

Jamie Bisotti
  • 2,605
  • 19
  • 23