0

For OTP length I am using 6, which I am taking from a variable

static final int OTP_LENGTH = 6;

@Size(min = OTP_LENGTH, max = OTP_LENGTH, message = "OTP Length should be {OTP_LENGTH} !!")
private String otp;

For which I am getting error mesage, if size is not of OTP_LENGTH.

"fieldErrors": [
    {
      "field": "password",
      "message": "Not a Base64 string !!"
    },
    {
      "field": "otp",
      "message": "OTP Length should be {OTP_LENGTH} !!"
    }
  ]

I also tried

@Size(min = OTP_LENGTH, max = OTP_LENGTH, message = "OTP Length should be ${OTP_LENGTH} !!")
private String otp;

But, OTP_LENGTH 6 is not setting.

Expectation :- "OTP Length should be 6 !!"

And, is there any other validator which takes only 1 length.

@Size, @Length can take max, @Size(max = OTP_LENGTH), but again min will be 0. If I will not override message, it will show length should be between 0 & 6.

5
  • What is the actual message which you do currently see? Commented Nov 2, 2019 at 8:30
  • "message": "OTP Length should be {OTP_LENGTH} !!". It's not replacing with 6 Commented Nov 2, 2019 at 8:31
  • I was following this one stackoverflow.com/questions/31020035/… Commented Nov 2, 2019 at 8:32
  • what about: OTP Length should be '${OTP_LENGTH}' ? Commented Nov 2, 2019 at 8:40
  • tried. No luck. Commented Nov 2, 2019 at 9:23

1 Answer 1

2

You have to use min and max fields from @Size annotation, e.g.:

@Size(min = OTP_LENGTH, max = OTP_LENGTH, message = "OTP Length must be between {min} and {max}")
private String otp;

For value, you can validatedValue property.

Sign up to request clarification or add additional context in comments.

2 Comments

can't we use OTP_LENGTH? And is there any alterative to take only one length. Size, Length takes min, max

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.