Struts2 time (HH:mm) mapping to action field - datetime

I have a simple struts2 from:
<s:form ...>
<s:textfield name="initialTime" maxlength="5" size="5" />
</s:form>
Where the user has to enter a time in HH:mm format. In the corresponding Action, I have a property:
private Date initialTime;
along with its getter/setter.
I am getting a validation problem, as the received initialTime (i.e. 10:20) is not understood as a valid Date.
What is the good approach to retrieve times ? Should I use a private String initialTime field instead of a Date ?
Thanks.

I think you'll have to change the property to a String and programatically transform it to whatever final format you want.
While I may see cases where the conversion from HH:mm to a Date may make sense, I think we can agree this is not a general scenario.
./alex

Related

DotVVM - DateTime json conversion not working

I'm new to DotVVM and just playing with it for the moment.
My model has a DateTime property
When i edit this in a form with
<dot:TextBox Text="{value: Datum}" size="20" />
Full datetime is shown like "2018-02-08T13:02:42.0000000"
Editing the string is possible, and sending the object back to the viewmodel works fine.
When i add a Formatstring like "yyyy-MM-dd" the property is NULL in the json sent to the app.
How can i fix this?
Is it possible to include my own JsonConverter for DateTime conversion? (In this example i only need the date property)
In order to use dates in TextBox, you need to add ValueType="DateTime" (and to use specific format, you can add FormatString="d" for example).

RangeValidator currency validation makes no sense

I'm having some issues using a RangeValidator in ASP.NET 4.5. The issue I'm facing is trying to validate a range of currency values.
Let's say I have the following RangeValidator:
<asp:RangeValidator ID="_rngValCustomAmount" runat="server"
CssClass="error-message" ErrorMessage="Please enter a valid amount."
ControlToValidate="_txtCustomAmount" MinimumValue="10.00"
MaximumValue="500.00" Type="Currency" />
When I enter a value of '450' the validation is passed, and I can submit the form. However, if I enter '$450' then the validation fails, even though I have set the Type property to Currency.
So, then I thought, OK, maybe I need to enter a currency Minimum and Maximum value, however when I change the control to:
MinimumValue="$10.00"
MaximumValue="$500.00"
I get the following .NET exception:
The value '$500.00' of the MaximumValue property of '_rngValCustomAmount' cannot be converted to type 'Currency'.
I don't really want to have to resort to using a CustomValidator for this, so that I don't have to write a JS function for something that should exist as the RangeValidator supports the currency type. Can anyone point out what I might be doing wrong?

mx:DateField and binding inside a Form

I have a Form with several FormItems and one of those include an mx:DateField. I am not sure how I should bind the DateField values. I have tried the following which none work:
<mx:DateField yearNavigationEnabled="true" text="#{dateValue}"/>
<mx:DateField yearNavigationEnabled="true" selectedDate="{dateValue}"/>
Now eventually I am trying to store/retrieve the date as a string in the db.
Any help or best practice would be appreciated
Thanks.
Have you tried
<mx:DateField yearNavigationEnabled="true" text="{dateValue as string}"/>
Try it without the #.
Also, this assumes that the date in the bound variable is in the "mm/dd/yyyy" format. If it's not then that could be the problem.
in your markup, try this (generally I put it right before my application's closing tag):
<mx:Binding source="dateValue" destination = "WhereEverItsGoing" twoway="true"/>
that should set up a two way binding between the datefield and the object you send to the service.
Maybe.

How can i validate date?

In a page i have taken a textbox used to enter date in mm/dd/yyyy formate.
I want that when user enter the incorrect format of the date then it show a message that incorrect format of date.
How can i validate textbox that only correct fomate of date can be entered by user.
Thanks in advance..
Instead of validating use calender control or there are loads of Jquery calenders, google it , Its better to provide user with date selection instead of date insertion. Make it idiot proof.
If Not IsDate(txtDate.Text) Then
'Error message code here...
End If
You could also use Date.TryParse().
Use DateTime.TryParseExact method.
DateTime dateValue;
if (DateTime.TryParseExact(textBox.Text, "mm/dd/yyyy",
CultureInfo.InvariantCulture, DateTimeStyles.None, out dateValue))
{
}

MaskedEditExtender, dates and Globalization

I want to use the MaskedEditExtender to mask short dates. The problem is that I want to mask the field depending on the user language settings.
This is working for a lot of cases, but for example for Latvian Culture (with format 9999.99.99. ) is not working.
<cc1:MaskedEditExtender ID="MaskedEditExtender1" runat="server" AutoComplete="True" MaskType="Date" TargetControlID="myTextbox" ClearMaskOnLostFocus="True"
OnInvalidCssClass="myInvalidCss" OnFocusCssClass="myOnFocusClass" Mask="99/99/9999" >
</cc1:MaskedEditExtender>
Is there a simple way to set the Mask property with the user culture mask format?
Am I missing something to do this easier?
Not sure why the extender won't recognize Latvian culture, but try looking at the overrides provided, such as CultureDateFormat and CultureDecimalPlaceholder. More info at the AJAX Control Toolkit sample website.
EDIT: Response to OP's comments:
I have not idea if this works, but it looks like you can get the short date format for a culture from the CultureInfo class, like this.
string shortDateFormat =
System.Globalization.CultureInfo.DateTimeFormat.ShortDatePattern
Take a look here for examples.

Resources