Weird behavior with Range DataAnnotation - asp.net-mvc

I have a Password field with [Range(5, 10)].
If I input a correct value between 5 and 10, it displays a message error anyway. What might be causing this?

the Range attribute is used to determine what the minimum and maximum numeric value for the field should be. If you are trying to set min/max length I would suggest using [StringLength(10, MinimumLength=5)] attribute to set this constraint on the property which would do what I think it is your looking to for

Related

Current value in Range validator ErrorMessage - MVC 5

This may be something really simply, but I just can't find any examples that do what I'm trying to do.
I'm trying to display the current value in my rangeattribute validator for my model.
I'm trying to decorating my property like so...
<Required>
<Display(Name:="Invoice(s) Amount")>
<Range(1, Decimal.MaxValue, ErrorMessage:="The value '{don't know what goes here}' is not valid for {0}.")>
<DataType(DataType.Currency)>
<DisplayFormat(ApplyFormatInEditMode:=False, DataFormatString:="{0:C2}")>
Public Property InvoiceAmount As Decimal
I've seen lots of examples where {1} and {2} are used to display the min and max for the range. However I haven't seen anything where the current value is displayed.
If I enter some non-numeric value, it displays the default messages as...
The value 'asdf' is not valid for Invoice(s) Amount.
This is why I'm assuming there aught to be some way to do it also for the range.
So essentially what I want to do is display the same kind of message when the value is a valid decimal, but is outside the allowed range, so it could be something like...
The value '-100' is not valid for Invoice(s) Amount.
It seems like it should be really simple.
I don't think this can be done with RangeAttribute, but create a class inherited form RangeAttribute and you can override the default behaviour and do what you like.

TValueListEditor with more columns

I'm using a TValueListEditor and i would like to make an IDE property editor like.
Each line can have few kinds of informations :
String
Date
Number
ComboList
I'm wondering how can i store the type of element and the format for each line.
I try to add 2 columns. But its not working
ValueListEditor.ColCount := 4;
ValueListEditor.Cell[4,2] := 'Test';
It shows the value Test in the value column (the second column instead of 4).
I was thinking to embeded the values that i need in an object that i will link (add) to each line. But i didnt' find the way to do so. Even i don't know if it's possible.
Does anyone have an idea ?
TValueListEditor is specifically designed to handle name=value pairs only. You can't add additional columns to it. However, you can specify the type of editor used for editing the value column. That is handled by the TItemProp.EditStyle property for each pair:
For String input, set the TItemProp.EditStyle to esSimple.
For ComboList input, set the TItemProp.EditStyle to esPickList, and then use the TItemProp.PickList property, or the TValueListEditor.OnGetPickList event.
For Numeric input, set the TItemProp.EditStyle to esSimple, and set the TItemProp.EditMask and TItemProp.MaxLength as needed. You can then convert the user's entered value to an Integer when needed.
For Date input, you are best off setting the TItemProp.EditStyle to esEllipsis and then use the TValueListEditor.OnEditButtonClick event to display your own TForm that has a TDateTimePicker on it. You could do something similar for numeric input if desired, using a TSpinEdit or similar component.

Bean Validation Min/Max wrong message

I'm using Min/Max Beanvalidation. Here is an example:
#Min(value = 100, message="too low")
#Max(value = 1000, message="too high")
private Integer example;
If i enter 99 i get the correct message "too low". If i enter 1001 i also get the correct message "too high". If i enter a very high number e.g. 10000000000 i get a generic message which i found out is this one: javax.faces.converter.BigIntegerConverter.BIGINTEGER={2}. So i suspect that if the user enters a number which is larger then the actual field type, he will get another message.
This is actually not what i want to achieve. I always want to show the user "too high". Is there a way to achieve this?
There are really two things going on, conversion and validation. In a first step JSF needs to take your string input and convert it to a number. This is where you get the error. Your value cannot be converted to an Integer. If conversions works, JSF populates your model and that's where validation kicks in. If validation then fails you get the defined Bean Validation messages. So what can you do:
Configure the JSF message for javax.faces.converter.BigIntegerConverter.BIGINTEGER={2} to be more descriptive
Change the datatype, for example use BigInteger. In this case the conversion from string to number will work
Use string in the bean and validate the string. You probably need then to convert to a number at a different point though, but that depends on you use case.
The maximum for Integer in java is 2^31 which is just over 2.1 billion. The input you used, 10 billion, is then beyond the maximum of an integer and would overflow the field, so it is not a valid given the field type, regardless of any validation you may have in place. you could switch the field type to be a BigInteger, then override the default validation messages to fit your needs, but that may be overkill given the purpose of your question. You can also have custom messages
Why not just limit the amount of characters in the inputfield in the frontend, for example
<h:inputText maxlength="4"/>
I'd guess it's possible to bypass if you really want, but I would'nt worry too much about the usability for someone hacking the site :-)

Why can't I enter a decimal point in a bound UITextView?

I'm binding a UITextView to a field of data type float, and it seems that the binding mechanism is being over-vigilant in validating my data such that I cannot actually enter floating point numbers.
Specifically, if I type "7" in my UITextView, the float field in the data model updates. If I paste "7.2" in the text view, it also updates. But if I type "7." (on the way to typing "7.2"), the binding fails with error:
MvxBind:Error:365.27 SetValue failed with exception - FormatException: Invalid format.
This is arguably correct, since "7." isn't a valid numeric string.
But it also discards the decimal point, and so I can't enter my number. It just leaves "7" in the text view. The weird thing is that this is ONLY affecting the decimal point/period character. If I type "7X", it doesn't bind, but it does let me type the X. What's going on here?
Just may be try to bind to string value and make a little manually in code string<=>float conversions? Also this should be much better way on monotouch specifically since based on the common .net functions rather than on the UI implementation.

What is the proper way to use check boxes to set flags in Rails?

I have an model containing a flags column. Inside the column is a number representing the flag state. All flags enabled is represented by 0b1111111, and individual options look something like 0b0010000 or 0b0000001. I want to have a form with check boxes (in this case 7 check boxes), and I want params[:flags] to be set to bitwise or of the selected boxes. Does anyone know a way to do this?
You should start by reading up on bitwise numbers as what you are storing isnt bitwise, you are simple flipping 7 0's and 1's in sequential order.
if you are looking to use bitwise you store an integer, with 7 checkbox your column will need to support a number of up to 256.
the check_box_tag has a 'checked' attribute, which you would simple pass the bitwise operator to which returns a boolean
check_box_tag("name", value, (persisted_value | value of this checkbox))

Resources