Formatting th:field in Thymeleaf - spring-mvc

I have a form input field in Thymeleaf. The field (bookingEntry.datefrom in the code snippet below) is type Date. I use a datepicker to allow the user to select and format the required date to enter the field. This is all fine.
However, I want the initial value of the date (which I have set to the current date) to be displayed in the format that I choose. So, how do I format the date initially shown in a th:field. th:value is ignored (Thymeleaf is getting the value from the backing object, as it should) and I can't seem to apply a format to the th:field.
Thymeleaf code is:
<input type="text" class="form-control getdate"
th:field="*{datefrom}" placeholder="Date From"
th:value="${#dates.format(bookingEntry.datefrom, 'dd-MMM-yyyy')}"/>
I'm sure that I could use a String which is initialise in any format that I choose, rather than a Date type, but I wondered if there was a way to format initial values in a th:field?
Many thanks

I missed the simple answer, simply because of my limited knowledge of Spring. I'm adding it here incase it helps any other novices like me.
The #DateTimeFormat annotation on the element in the object being passed to the form does the job. It ensures that the Date object is formatted in the way that you wish (irrespective of whether you are using Thymeleaf or not).
In the example above, within the bookingEntry object
#Temporal(DATE)
#DateTimeFormat (pattern="dd-MMM-YYYY")
private Date datefrom;

In case your date is null, it will give you error. We have to check the value before we parse the date.
<input type="text" name="date"
th:value="${user.dateOfBirth}?${#dates.format(user.dateOfBirth, 'dd-MM-yyyy')}:''"
placeholder="dd-mm-yyyy" id="pickyDate"/>

Related

How to change date format in Email Template of Dynamics365?

I want to get the date in this format -
MM-dd-yy.
For example: 10-12-23
I'm writing this code but don't getting the desired solution -
{<entity_name>:<attribute>/#date;} Check-In Link
What should I write in the date part?
I'm afraid the only way to make it work is to set the user's locale to a locale that uses MM-dd-yy format.
Alternatively, it's possible to create a custom string field, populate that field with a properly formatted date and use the value of it in your template.

Symfony2 - Array of data

This is the last problem I am facing with Symfony2. So, I am using a normal form, not the form builder.
I have one table called alert which has the standard fields. I have a second table called booking_class which has an alert key in it which is linked to the alert table. Its a one to many relationship. In my form, I have something like
<input type="text" name="na_booking_class[]">
<input type="text" name="na_booking_class[]">
So this input is placed into an array. Now in my controller, I presume that this will give me my array of data
$alert->setBookingClass($request->get('na_booking_class'));
However, setBookingClass only accepts a String. So I need to somehow loop this array and pass the String values to setBookingClass.
How would this be achieved?
Thanks
If you get an array from this $request->get('na_booking_class'); then you can use:
$arrBooking = $request->get('na_booking_class');
foreach($arrBooking as $booking) {
$alert->setBookingClass($booking);
}
Try with this.

ASP.NET Differentiate between Date and Datetime Fields

I have an ASP.NET form(MVC3/Razor) and on the the form I want to alter how a date field displays from a datetime field. I.e. in some of the data columns the time is significant and I want the date and time to show, in others it is irrelevant and I just want the date to display. Is this possible?
Thanks in advance
Chris
Two ways: 1) Using a UIHint attribute to tell the field whether or not to render as a Date or DateTime and then having an EditorTemplate to differentiate the two. Or 2) Use the EditorFor extension method and tell it explicitly which EditorTemplate to use.
The same can go for DisplayTemplates when you want to truncate the time.
Here's an example from SO: ASP.NET MVC Editor-Templates/UIHint with parameters
You can use String.Format to format the date and time in exactly the way you want.
string.Format("{0:dd-MMM-yy}", yourDate);
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
You can format each appropriate column as you want. For example:
#string.Format("{0:MM/dd/yyyy}",DateTimeField)
and/or
#string.Format("{0:MM/dd/yyyy hh:mm:ss}",DateTimeField)

Display value in Date field irrespective of date format

I have an mx:DateField in my Flex UI that has a formatString="dd.mm.yyyy" attached to it. However, the initial value for that field may not be in the format specified/defined for the DateField (due to legacy reasons). So, currently, if I just set that value (text) on the DateField, the field is shown empty. I think because it fails to accept the value in a format different than what's configured.
I need to (somehow) be able to display the incoming value in the date field. Is that possible? If yes, how?
P.S. : The incoming value's format will not be known, so I cannot transform it to my desired format :(.
Parse the value into a date first, then use the dateField's dateToString method to set the text.

How can I set a schema.Datetime field to None with Dexterity

I'm writing a simple content type with Dexterity to manage customers, beside the usuals fields, eg name, company, phone...
I've also added a Datetime field to store when the first meeting with
customers has been held, lets call it 'firstmeeting', which I defined
in my interface ICustomers as:
firstmeeting = schema.Datetime(
title=_(u"First Meeting"),
required=False,
)
Now, I notice that when I saved a new Customer document the firstmeeting
field has been filled with the current date even if I don't set any date
in the form, which is not what I want because no meeting with the
customer has been held yet. So I'd like to know how to set a None value
for this field so nothing will be displayed.
I've been trying to use a custom class has explained by Martin Aspeli in
http://plone.org/products/dexterity/documentation/manual/developer-manual/advanced/classes
but I don't know how to check the user input and set None value if nothing
was typed in.
Thanks
Have you tried default=None?
I found out what was happening in my code.
Actually the problem was in the template view.pt where I used toLocalizedTime() function,
which was converting the None value to the current date, so I added a tal:condition to print the date value.
<span id="form-widgets-firstmeeting" class="datetime-widget datetime-field"
tal:condition="context/firstmeeting"
tal:content="python:context.toLocalizedTime(context.firstmeeting)" />

Resources