help with custom validator
hey guys. can smb help me out with writing my won validation annotation? i have entity
Payment
and DTO PaymentDTO
and they have private BigDecimal amount
field. i want to validate it bc i want this field's length to be 7 positions at maximum. so far i have this:
1. what to write instead of ???
in my AmountValidator
? should it be data type of my field? i.e. BigDecimal
?
2. how to check if my field's max length is less than 7 positions in isValid
method in the AmountValidator
?
thanks in advance.18 Replies
⌛
This post has been reserved for your question.
Hey @bambyzas! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
Hi there,
1. It should be the same type of an object or Java primitive types of the value for which you do logics and comparing in the
isValid(BigDecimal amountField, ConstraintValidatorContext ctx
medthod. In your case it's BigDecimal
2. Convert BigDecimal
to String
and check the length of string so it would be no bigger than seven. You can also use regex.
Spoon-feeding:
Test:
Results:
thanks. but i just found out that i can use annotations: https://www.baeldung.com/javax-bigdecimal-validation
Baeldung
Javax BigDecimal Validation | Baeldung
A quick and practical overview of BigDecimal validation using Javax.
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
would it make more sense rather than writing my own?
As you wish. Just test that those annotations really work on
BigDecimal
.
I'll test with @Digits(integer=7, fraction=6)
. This allows 13 length number and you need 7 digits. If you choose @Digits(integer=7, fraction=0)
then you have 7 digits integer part but you will not be able to enter fractional part. Your 99999.99
would throw an exception.
I would choose the AmountValidator
. You give then any freedom to a BigDecimal to have any amount of fractional part and overall length of 7.
And these @Digts(..
can set only exact amount for integer and fractional parts.but thats what i need
I have tested with
@Digits(integer=7, fraction=0)
:
And made a test:
And got an exception:
With those @Digits(integer=7, fraction = 0)
I can enter a number like one million and take all 7 digits.
But we cannot enter a decimal number with fractional part.
So that's it. I choose AmountValidator
.
You can use BigDecimal
and use @Digits
only if you know how big the number can be and how many digits does the fractional part takes.thats bc u didnt specify
fraction
fractional part will always be two digits in my casesif you would specify 2, like
@Digits(integer=7, fraction = 2)
the overall positions is 7+2 = 9
You said you need 7 positions at maximum
If you use @Digits
then your integer part is at maximum 5 then
@Digits(integer=5, fraction = 2)
- now you will have 7 positions.
But you integer number cannot exceed 99999.01yep,. thats correct
And this:
will give you the possibility to enter such
BigDecimal
s like 0.123456
to 9999999
using all 7 positions for digits
no matter integer part and fractional part99999.99 to be exact
Yea
I like the 5.2 approach tbh
If that's all he needs
Use as you need it. You've just said that you need only 2 positions for fractional part. Then you will have 5 positions for integers at max and will have 7 positions overall.
@Digits(integer=5, fraction = 2)
will serve you well for it.💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.