Validate if greater then zero - using valdr-bean-validation - bean-validation

I would like to make sure that totalAmount is greater then 0.
I have following code to in my Java bean:
#NotNull(message = "Total amount must be entered")
#Digits(integer = 8, fraction = 2, message = "Please enter a valid amount less than 99,999,999.99")
#DecimalMin(value = "0.01", message = "Please enter a valid amount more than 0")
private BigDecimal totalAmount;
Unfortunately valdr-bean-validator (tool to use JSR-303 with AngularJS) doesn't support #DecimalMin or #DecimalMax. Is there any other way to create the rule for values greater then zero?
EDIT: please note that totalAmount is BigDecimal

This should do.
#DecimalMin(value = "0", inclusive = false)
by default incluside is true

I couldn't solve it with JSR-303. I finally implemented custom valdr validator.
It is shame that such common case (value equal to zero) is not supported.

Related

Format zero currency value with {0:C} in VB.Net

I am trying to format a zero currency value as an empty string, so that when the currency value is 0.00 then an empty string gets displayed rather than $0.00.
This code is part of an ASP.Net app that will display currency value to end user.
I have used following code to achieve this goal.
Question : Is it possible to achieve this by just using {0:C} format string or another version of this format string instead of using if then else coding for this? If I use ###,###,###.## as the data format string then an empty string shows for zero currency value and also I get rid of the if then else coding but for non-zero values no currency symbol shows.
If Double.Parse(Decimal.Parse(CDec(currencyValue))) = 0 Then
charValue = Nothing
Else
charValue = String.Format("{0:C}", CDec(currencyValue))
End If
UPDATE
I ended up using the following code, which is working fine. If is better than IIf because it does short-circuiting, which means that IIf will evaluate all expressions whether the condition is true or false but If will evaluate the first expression only if condition is true and evaluate the second expression only if condition is false.
Dim d As Decimal
Decimal.TryParse(currencyValue, d)
charValue = If(d = 0D, Nothing, String.Format("{0:C}", d))
I don't think there is a way using formatting to display an empty string.
But you can write it like:
charValue = If( currencyValue = 0D, "", currencyValue.ToString("C") )
using the If Operator (Visual Basic).
Also this is something I would not do:
If Double.Parse(Decimal.Parse(CDec(currencyValue))) = 0 Then
If currencyValue is Decimal:
If (currencyValue = 0D) Then
If currencyValue is Double:
If (currencyValue = 0R) Then
Also, if you are using a database and this is a Sql Server mind SQL Server Data Type Mappings
I don't think you can when using C or the other similar standard formats, since they are already defining a culture-specific format that will include a format for zero.
But if you specify your own custom format, you can specify three different formats separated by ;s, one each for positive numbers, negative numbers, and zero, respectively.
For example (giving an empty string for the zero format, resulting in blank zeroes):
charValue = String.Format("{0:#,##0.00;-#,##0.00;""""}", CDec(currencyValue))
And from what I can see, omitting the format for negative gives a default that matches the positive, whereas omitting the format for zero gives blank, which is what you're looking for, so this should be sufficient as well:
charValue = String.Format("{0:#,##0.00;;}", CDec(currencyValue))
(Using whichever custom format you wish.)
UPDATE: You can get the current currency symbol and manually put it into your custom format. IE:
Dim symbol = CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol
charValue = String.Format("{0}{1:#,##0.00;;}", symbol, CDec(currencyValue))
From the sound of it, though, I think I would actually recommend doing basically what you started with, maybe with an extension method.
<Extension>
Public Function ToCurrencyString(pValue As Decimal) As String
Return IIf(pValue = 0, "", pValue.ToString("C"))
End Function
Dim someValue As Decimal = 1.23
Console.WriteLine(someValue.ToCurrencyString())
This gives you exactly what you're looking for. The exact same format as C gives, but with blank zeroes.

How to display date in RDLC in another language

I want to display date in RDLC(Report Definition Language Client-side) as १०/०७/२०१० instead 10/07/2010. How can i do this?
I believe it is not possible - at least not without effort on your side. Native digits are defined in .NET FW like:
var culture = new CultureInfo("km-KH");
for (var i = 0; i < culture.NumberFormat.NativeDigits.Length; i++) {
Console.Out.WriteLine(culture.NumberFormat.NativeDigits[i]);
}
BUT native digits formatting is not supported (yet).
Reference:
http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.digitsubstitution.aspx
The DigitSubstitution property is reserved for future use. Currently,
it is not used in either parsing or formatting operations for the
current NumberFormatInfo object.
You still can manage to do it on your own - i.e. add Report->Report properties->Code
Function Khmer(value)
value = Replace(value,"0","០")
value = Replace(value,"1","១")
value = Replace(value,"2","២")
value = Replace(value,"3","៣")
value = Replace(value,"4","៤")
value = Replace(value,"5","៥")
value = Replace(value,"6","៦")
value = Replace(value,"7","៧")
value = Replace(value,"8","៨")
value = Replace(value,"9","៩")
Khmer = value
End Function
Note - report code uses vb syntax (always) - dunno why.
And in your report add expression for the field you want to encode to Khmer native digits:
=Code.Khmer(Fields!DateCreated.Value)

Overwriting default Hibernate Validator messages

I want to overwrite the default Hibernate Validator messages in a server faces web app, so I read this part of the documentation: https://docs.jboss.org/hibernate/validator/5.0/reference/en-US/html/chapter-message-interpolation.html#section-message-interpolation
I created two files inside the src/ folder: ValidationMessages.properties and ValidationMessages_de_DE.properties. The file contents are:
javax.validation.constraints.AssertFalse.message = must be false
javax.validation.constraints.AssertTrue.message = must be true
javax.validation.constraints.DecimalMax.message = must be less than ${inclusive == true ? 'or equal to ' : ''}{value}
javax.validation.constraints.DecimalMin.message = must be greater than ${inclusive == true ? 'or equal to ' : ''}{value}
javax.validation.constraints.Digits.message = numeric value out of bounds (<{integer} digits>.<{fraction} digits> expected)
javax.validation.constraints.Future.message = must be in the future
javax.validation.constraints.Max.message = must be less than or equal to {value}
javax.validation.constraints.Min.message = must be greater than or equal to {value}
javax.validation.constraints.NotNull.message = may not be null
javax.validation.constraints.Null.message = must be null
javax.validation.constraints.Past.message = must be in the past
javax.validation.constraints.Pattern.message = must match "{regexp}"
javax.validation.constraints.Size.message = size must be between {min} and {max}
org.hibernate.validator.constraints.CreditCardNumber.message = invalid credit card number
org.hibernate.validator.constraints.Email.message = not a well-formed email address
org.hibernate.validator.constraints.Length.message = length must be between {min} and {max}
org.hibernate.validator.constraints.NotBlank.message = may not be empty
org.hibernate.validator.constraints.NotEmpty.message = may not be empty
org.hibernate.validator.constraints.Range.message = must be between {min} and {max}
org.hibernate.validator.constraints.SafeHtml.message = may have unsafe html content
org.hibernate.validator.constraints.ScriptAssert.message = script expression "{script}" didn't evaluate to true
org.hibernate.validator.constraints.URL.message = must be a valid URL
org.hibernate.validator.constraints.br.CNPJ.message = invalid Brazilian corporate taxpayer registry number (CNPJ)
org.hibernate.validator.constraints.br.CPF.message = invalid Brazilian individual taxpayer registry number (CPF)
org.hibernate.validator.constraints.br.TituloEleitor.message = invalid Brazilian Voter ID card number
I am using a german version of Windows, so the messages are by default in german. After creating the files they should be in english, but they are still in german.
What am I doing wrong?
Okay, the correct folder is /src/main/java/.

Input string was not in a correct format when adding session variables

I am trying to add some session values and if any of the value have a decimal value ie 12.50 I get an error that Input string was not in a correct format.?
Dim Res As Integer = Convert.ToInt32(Session("ConsultingFeeAmount")) + Convert.ToInt32(Session("FoodAndBeverageAmount"))
TotalAmount = Environment.NewLine + "Total Amount: " + Session("ConsultingFeeAmount") + Session("FoodAndBeverageAmount")
TotalAmount = "Total Amount " + Res.ToString
Use a TryParse method from the Decimal class
Dim consultAmt As Decimal
Dim foodAmt As Decimal
Decimal.TryParse(Session("ConsultingFeeAmount"), consultAmt))
Decimal.TryParse(Session("FoodAndBeverageAmount"), foodAmt))
Dim Res As Decimal = consultAmt + foodAmt
TotalAmount = Environment.NewLine & "Total Amount: " & _
consultAmt.ToString() & " " & foodAmt.ToString()
TotalAmount = "Total Amount " & Res.ToString
The Decimal.TryParse analize the input string and set the second parameter with the converted value if it is possible to convert the string to a decimal. If not the method doesn't rises any exceptions and the second parameter is let alone to its default value.
EDIT
The OP says that after the change initially suggested now it has an error message that says:
Conversion from string " Total Amount: 12.50 13.00" to type 'Double'
is not valid
The problem was the + operator used to concatenate strings when Option Strict is OFF. In that case the VB compiler confuses the meaning of the operator and tries to sum two numeric values. I really suggest to use Option Strict On and Option Explicit On because this will force your code to be more precise (no implicit conversion of types). Of course if you make this change you need an extensive retest of your application
You can read about this problem in this question/answer link
I think your problem lies in the code Convert.ToInt32. You can't pass in a decimal number there. It's expecting an integer. That occurs more than once in your code.
I am guessing the value you passed to Convert.ToInt32 are not a valid numeric ones. Make sure you check the session values are empty or not before using that.
if Session("ConsultingFeeAmount") IsNot Nothing Then
' Now use this session variable
End If
I don't know if vb.net is different than C#, but Session returns an object not a typed value. You will need to cast Session("ConsultingFeeAmount") to a decimal.
CType(Session("ConsultingFeeAmount"), Decimal)
or
CType(Session("ConsultingFeeAmount"), Integer)

how do I convert textbox value to string?

I know this is simple but i am lost as to how to approach it. Iam a new newbie. Please have it on me as i am new here.
I have a textbox field called ClientsBalance.
This balance can is usually in money order with a set amount. The client is also allowed to pay by Debit Cards.
Here goes:
Dim paymentType As TextBox
otherPyment As String = "Debit Card"
If paymentType.Text <> "1250" Then
paymentType = "OtherPayment"
else
paymentType = gridview1.FindControl("paymentType" & CStr(1))
end if
Everything was working before the validation.
Now, I get the following error:
Value of type 'String' cannot be converted to 'System.Web.UI.WebControls.TextBox'
Is there a way I can cast this line:
paymentType = "OtherPayment"
??
Thank you so much experts.
you need to say
paymentType.Text = "OtherPayment".
You just left out the ".Text"
Try changing this:
paymentType = "OtherPayment"
To this:
paymentType.Text = "OtherPayment"

Resources