s:textfield and date format as dd/MM/yyyy

View: New views
10 Messages — Rating Filter:   Alert me  

s:textfield and date format as dd/MM/yyyy

by Carl Ballantyne :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi All,

I have a Person object with a birthday attribute which is a Date. In  
the form I have a textfield to enter the value. I want the field to  
display the date in the format dd/MM/yyyy and the validation to apply  
using that format. However everything I am trying does not work.

I tried putting "format.date = {0,date,dd/MM/yyyy}" in the properties  
(http://struts.apache.org/2.1.8/docs/formatting-dates-and-numbers.html) as I  
am apparently supposed to but this does not display the date in the  
correct format via the s:date tag or the s:textfield tag and the  
validation does not work. Via the s:date I get "Nov 16, 1976 12:00:00  
AM" and via s:textfield I get "11/16/76 12:00:00 AM.000". If I try and  
put a date in the format dd/MM/yyyy it tells me it is in an invalid  
format. It only accepts MM/dd/yyyy.

I also tried adding "struts.date.format = dd/MM/yyyy" to the  
properties file for my package.  
(http://struts.apache.org/2.1.8/docs/date.html). This seems to work a  
bit better. The date is displayed in the correct format via the s:date  
tag. However the s:textfield displays it as "11/16/76 12:00:00  
AM.000". And the validation works the same as before, ie only accepts  
MM/dd/yyyy dates when I really want dd/MM/yyyy.

I am not using a custom Date Converter because the guide says not to  
use it to display i18n formatted dates. Do I need to resort to this?

It just seems I am doing something wrong as it should not be this  
difficult. How do you guys deal with converting date strings?

Cheers,
Carl.



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@...
For additional commands, e-mail: user-help@...


Re: s:textfield and date format as dd/MM/yyyy

by Siddiq Syed :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

As far as date validation are concerned , declare the date variable as string in your view bean,

Add regular expression validation in the validation file.

<field-validator type="regex">
(((((((0[13578])|(1[02]))[\/]((0[1-9])|([12]\d)|(3[01])))|(((0[469])|(11))[\/]?((0[1-9])|([12]\d)|(30)))|((0?2)[\/]((0[1-9])|(1\d)|(2[0-8]))))[\/](((19)|([1-9][0-9]))([\d][\d]))))|((0?2)[\/](29)[\/](((19)|([1-9][0-9]))(([02468][048])|([13579][26])))))$
</field-validator>

once validation passes you can convert the string to date and used the same in the service layer.

Hope its work for you.

carl ballantyne wrote:
Hi All,

I have a Person object with a birthday attribute which is a Date. In  
the form I have a textfield to enter the value. I want the field to  
display the date in the format dd/MM/yyyy and the validation to apply  
using that format. However everything I am trying does not work.

I tried putting "format.date = {0,date,dd/MM/yyyy}" in the properties  
(http://struts.apache.org/2.1.8/docs/formatting-dates-and-numbers.html) as I  
am apparently supposed to but this does not display the date in the  
correct format via the s:date tag or the s:textfield tag and the  
validation does not work. Via the s:date I get "Nov 16, 1976 12:00:00  
AM" and via s:textfield I get "11/16/76 12:00:00 AM.000". If I try and  
put a date in the format dd/MM/yyyy it tells me it is in an invalid  
format. It only accepts MM/dd/yyyy.

I also tried adding "struts.date.format = dd/MM/yyyy" to the  
properties file for my package.  
(http://struts.apache.org/2.1.8/docs/date.html). This seems to work a  
bit better. The date is displayed in the correct format via the s:date  
tag. However the s:textfield displays it as "11/16/76 12:00:00  
AM.000". And the validation works the same as before, ie only accepts  
MM/dd/yyyy dates when I really want dd/MM/yyyy.

I am not using a custom Date Converter because the guide says not to  
use it to display i18n formatted dates. Do I need to resort to this?

It just seems I am doing something wrong as it should not be this  
difficult. How do you guys deal with converting date strings?

Cheers,
Carl.



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org

Re: s:textfield and date format as dd/MM/yyyy

by Greg Lindholm-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

<field-validator type="regex">
>
> (((((((0[13578])|(1[02]))[\/]((0[1-9])|([12]\d)|(3[01])))|(((0[469])|(11))[\/]?((0[1-9])|([12]\d)|(30)))|((0?2)[\/]((0[1-9])|(1\d)|(2[0-8]))))[\/](((19)|([1-9][0-9]))([\d][\d]))))|((0?2)[\/](29)[\/](((19)|([1-9][0-9]))(([02468][048])|([13579][26])))))$
> </field-validator>
>
>
>
IMHO this is unreadable and unmaintainable and should never be used (even if
it does work).

You would be better off changing your 'date' property to be a String and
handle validation in a validate() method using a DateFormat object.

Re: s:textfield and date format as dd/MM/yyyy

by Siddiq Syed :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

But I guess there is no direct validation for a date in struts 2.

If you give date validation like mention below

<field-validator type="date">
      01/01/1990
      01/01/2000
      <message>Joining date must be supplied between ${min} and ${max}</message>
    </field-validator>

It will check for the range , not the validation , like invalid value and leap year.

As far as what I saw is that reg expression will slove it easily but its not readable and maintainable.


Greg Lindholm-2 wrote:
<field-validator type="regex">
>
> (((((((0[13578])|(1[02]))[\/]((0[1-9])|([12]\d)|(3[01])))|(((0[469])|(11))[\/]?((0[1-9])|([12]\d)|(30)))|((0?2)[\/]((0[1-9])|(1\d)|(2[0-8]))))[\/](((19)|([1-9][0-9]))([\d][\d]))))|((0?2)[\/](29)[\/](((19)|([1-9][0-9]))(([02468][048])|([13579][26])))))$
> </field-validator>
>
>
>
IMHO this is unreadable and unmaintainable and should never be used (even if
it does work).

You would be better off changing your 'date' property to be a String and
handle validation in a validate() method using a DateFormat object.

RE: s:textfield and date format as dd/MM/yyyy

by Kawczynski, David :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

That is unreadable.  What about this, it's much easier on the eyes:
(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d

From
http://www.regular-expressions.info/regexbuddy/datemmddyyyy.html


>
> Greg Lindholm-2 wrote:
> >
> > <field-validator type="regex">
> >>
> >>
> (((((((0[13578])|(1[02]))[\/]((0[1-9])|([12]\d)|(3[01])))|(((0
> [469])|(11))[\/]?((0[1-9])|([12]\d)|(30)))|((0?2)[\/]((0[1-9])
> |(1\d)|(2[0-8]))))[\/](((19)|([1-9][0-9]))([\d][\d]))))|((0?2)
[\/](29)[\/](((19)|([1-9][0-9]))(([02468][048])|([13579][26]))))> )$

> >> </field-validator>
> >>
> >>
> >>
> > IMHO this is unreadable and unmaintainable and should never
> be used (even
> > if
> > it does work).
> >
> > You would be better off changing your 'date' property to be
> a String and
> > handle validation in a validate() method using a DateFormat object.
> >
> >
>
> --
> View this message in context:
> http://old.nabble.com/s%3Atextfield-and-date-format-as-dd-MM-y
yyy-tp26197512p26202747.html
> Sent from the Struts - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@...
> For additional commands, e-mail: user-help@...
>
>
Notice:  This e-mail message, together with any attachments, contains information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station, New Jersey, USA 08889), and/or its affiliates Direct contact information for affiliates is available at http://www.merck.com/contact/contacts.html) that may be confidential, proprietary copyrighted and/or legally privileged. It is intended solely for the use of the individual or entity named on this message. If you are not the intended recipient, and have received this message in error, please notify us immediately by reply e-mail and then delete it from your system.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@...
For additional commands, e-mail: user-help@...


RE: s:textfield and date format as dd/MM/yyyy

by Siddiq Syed :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Does it checks for leap year ?

(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d

I guess this does not check for leap year, If the user enter the leap year the framework passes the validation successfully. which it should not and give you the error message.


Kawczynski, David wrote:
That is unreadable.  What about this, it's much easier on the eyes:
(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d

From
http://www.regular-expressions.info/regexbuddy/datemmddyyyy.html


>
> Greg Lindholm-2 wrote:
> >
> > <field-validator type="regex">
> >>
> >>
> (((((((0[13578])|(1[02]))[\/]((0[1-9])|([12]\d)|(3[01])))|(((0
> [469])|(11))[\/]?((0[1-9])|([12]\d)|(30)))|((0?2)[\/]((0[1-9])
> |(1\d)|(2[0-8]))))[\/](((19)|([1-9][0-9]))([\d][\d]))))|((0?2)
[\/](29)[\/](((19)|([1-9][0-9]))(([02468][048])|([13579][26]))))> )$
> >> </field-validator>
> >>
> >>
> >>
> > IMHO this is unreadable and unmaintainable and should never
> be used (even
> > if
> > it does work).
> >
> > You would be better off changing your 'date' property to be
> a String and
> > handle validation in a validate() method using a DateFormat object.
> >
> >
>
> --
> View this message in context:
> http://old.nabble.com/s%3Atextfield-and-date-format-as-dd-MM-y
yyy-tp26197512p26202747.html
> Sent from the Struts - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
Notice:  This e-mail message, together with any attachments, contains information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station, New Jersey, USA 08889), and/or its affiliates Direct contact information for affiliates is available at http://www.merck.com/contact/contacts.html) that may be confidential, proprietary copyrighted and/or legally privileged. It is intended solely for the use of the individual or entity named on this message. If you are not the intended recipient, and have received this message in error, please notify us immediately by reply e-mail and then delete it from your system.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org

Re: s:textfield and date format as dd/MM/yyyy

by Greg Lindholm-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Still a bad idea as it allows invalid dates (e.g. Nov. 31) and does nothing
for leap years.

If you want to know if a date is valid use DateFormat.parse().


On Wed, Nov 4, 2009 at 2:16 PM, Kawczynski, David <
david_kawczynski@...> wrote:

> That is unreadable.  What about this, it's much easier on the eyes:
> (0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d
>
> From
> http://www.regular-expressions.info/regexbuddy/datemmddyyyy.html
>
>
> >
> > Greg Lindholm-2 wrote:
> > >
> > > <field-validator type="regex">
> > >>
> > >>
> > (((((((0[13578])|(1[02]))[\/]((0[1-9])|([12]\d)|(3[01])))|(((0
> > [469])|(11))[\/]?((0[1-9])|([12]\d)|(30)))|((0?2)[\/]((0[1-9])
> > |(1\d)|(2[0-8]))))[\/](((19)|([1-9][0-9]))([\d][\d]))))|((0?2)
> [\/](29)[\/](((19)|([1-9][0-9]))(([02468][048])|([13579][26]))))> )$
> > >> </field-validator>
> > >>
> > >>
> > >>
> > > IMHO this is unreadable and unmaintainable and should never
> > be used (even
> > > if
> > > it does work).
> > >
> > > You would be better off changing your 'date' property to be
> > a String and
> > > handle validation in a validate() method using a DateFormat object.
> > >
> > >
> >
> > --
> > View this message in context:
> > http://old.nabble.com/s%3Atextfield-and-date-format-as-dd-MM-y
> yyy-tp26197512p26202747.html<http://old.nabble.com/s%3Atextfield-and-date-format-as-dd-MM-y%0Ayyy-tp26197512p26202747.html>
> > Sent from the Struts - User mailing list archive at Nabble.com.
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@...
> > For additional commands, e-mail: user-help@...
> >
> >
> Notice:  This e-mail message, together with any attachments, contains
> information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station, New
> Jersey, USA 08889), and/or its affiliates Direct contact information for
> affiliates is available at http://www.merck.com/contact/contacts.html)
> that may be confidential, proprietary copyrighted and/or legally privileged.
> It is intended solely for the use of the individual or entity named on this
> message. If you are not the intended recipient, and have received this
> message in error, please notify us immediately by reply e-mail and then
> delete it from your system.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@...
> For additional commands, e-mail: user-help@...
>
>

Re: s:textfield and date format as dd/MM/yyyy

by Siddiq Syed :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

When you have a validation file  like actionname-aliase-validation.xml and you want to carryout date validation in that as other validation , there is no direct way to do that.

Handling DateFormat.parse() is more java specific way and can be done in any layer of the application.
The front-end validation need to be happen in the validation file, which might be simple.

Greg Lindholm-2 wrote:
Still a bad idea as it allows invalid dates (e.g. Nov. 31) and does nothing
for leap years.

If you want to know if a date is valid use DateFormat.parse().


On Wed, Nov 4, 2009 at 2:16 PM, Kawczynski, David <
david_kawczynski@merck.com> wrote:

> That is unreadable.  What about this, it's much easier on the eyes:
> (0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d
>
> From
> http://www.regular-expressions.info/regexbuddy/datemmddyyyy.html
>
>
> >
> > Greg Lindholm-2 wrote:
> > >
> > > <field-validator type="regex">
> > >>
> > >>
> > (((((((0[13578])|(1[02]))[\/]((0[1-9])|([12]\d)|(3[01])))|(((0
> > [469])|(11))[\/]?((0[1-9])|([12]\d)|(30)))|((0?2)[\/]((0[1-9])
> > |(1\d)|(2[0-8]))))[\/](((19)|([1-9][0-9]))([\d][\d]))))|((0?2)
> [\/](29)[\/](((19)|([1-9][0-9]))(([02468][048])|([13579][26]))))> )$
> > >> </field-validator>
> > >>
> > >>
> > >>
> > > IMHO this is unreadable and unmaintainable and should never
> > be used (even
> > > if
> > > it does work).
> > >
> > > You would be better off changing your 'date' property to be
> > a String and
> > > handle validation in a validate() method using a DateFormat object.
> > >
> > >
> >
> > --
> > View this message in context:
> > http://old.nabble.com/s%3Atextfield-and-date-format-as-dd-MM-y
> yyy-tp26197512p26202747.html<http://old.nabble.com/s%3Atextfield-and-date-format-as-dd-MM-y%0Ayyy-tp26197512p26202747.html>
> > Sent from the Struts - User mailing list archive at Nabble.com.
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
> Notice:  This e-mail message, together with any attachments, contains
> information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station, New
> Jersey, USA 08889), and/or its affiliates Direct contact information for
> affiliates is available at http://www.merck.com/contact/contacts.html)
> that may be confidential, proprietary copyrighted and/or legally privileged.
> It is intended solely for the use of the individual or entity named on this
> message. If you are not the intended recipient, and have received this
> message in error, please notify us immediately by reply e-mail and then
> delete it from your system.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: s:textfield and date format as dd/MM/yyyy

by Greg Lindholm-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Nov 4, 2009 at 2:54 PM, Siddiq Syed <siddiq_sa@...> wrote:

>
> When you have a validation file  like actionname-aliase-validation.xml and
> you want to carryout date validation in that as other validation , there is
> no direct way to do that.
>

Correct, this is a limitation, there currently is no way to
(fully/safely/correctly) validate a localized Date using declarative
validation.
Fortunately Struts provides multiple mechanisms for validation and
conversion so if one doesn't provide what you need you have options
available to you.


>
> Handling DateFormat.parse() is more java specific way and can be done in
> any
> layer of the application.
>

Well Struts is a Java framework so I don't see this as a limitation.


> The front-end validation need to be happen in the validation file, which
> might be simple.
>
>
If you really want to do front-end validation I'm sure you can find a good
JavaScript library that provide this functionality. But when the date string
gets to your action you are still going to need to convert and validate it
before you use it.

Re: s:textfield and date format as dd/MM/yyyy

by Carl Ballantyne :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks for all the feedback.

Greg - I like your suggestion about using the DateFormat to parse the String. But I don't like having to change the type in my domain classes to Strings (or add String equivalents like birthdayStr) just so they work better with Struts. This doesn't feel right to me. I am at the start of a big project and am really trying hard to do things properly so they don't multiply through the app.

What I will probably end up doing, based on lack of another solution, is implement a Custom DateConverter and use a Person-conversion.properties file to link it to my birthday field. I will use the DateFormat inside the DateConverter which could then throw a conversion error if the date is not valid and there is a parse error. This way I keep it consistent, within the framework and don't need to put extra code in actions converting strings to dates.

On a second reading of Type Conversion in the docs (http://struts.apache.org/2.1.8/docs/type-conversion.html) maybe I was not understanding it correctly and expecting too much magic from Struts 2. It looks to me that now if you want to display a date - then use the struts.date.format like I was but if you want to be converting from strings to dates then it only uses uses the "SHORT format for the Locale associated with the current request". And anything else you will need to use a converter.