From: Rhino on
I'm trying to think of a good design for a hypothetical validation of a
date and would appreciate some suggestions.

Suppose I have a form and one of the things that needs to be entered on the
form is a date. Let's say that I want the date to look like this: YYYY-MM-
DD, where the year is in the range 1 to 9999, the month is in the range 1
to 12, and the day is in the range 1 to 28/29/30/31, depending on what
month it is and whether it is a leap year. (I've already written and tested
all the if statements and/or REGEXs that will verify that all parts of the
date are valid so I don't need any help with that part.)

I'd like the form validation to return a clear and specific error message
if the date fails to meet any of those conditions so that the user knows
exactly what to correct. For example, "the day portion of the date can't be
29 if the month portion is February and the year is not a leap year". A
generic message that simply says "invalid date" for all possible errors in
the date field just doesn't cut it for me.

My current design, recognizing that the date might be find or might be
invalid, returns TWO items in a two-element Object array: first, a boolean
that indicates whether the date was valid and second, if there is an error
in the date, a specific error message.

This code works fine but it feels wrong to return two items from a method.
I suppose I could just return a message string and, if the message string
is blank, let that be the way the calling program knows that the date was
valid. Or would it be better to have the date validation method throw
exceptions containing the specific error message and then display the error
message in the program that calls the validation message? That's the
approach I'm leaning toward.

--
Rhino

--- news://freenews.netfront.net/ - complaints: news(a)netfront.net ---
From: Steve W. Jackson on
In article <Xns9D3F6C815F067noofflinecontactplea(a)202.177.16.121>,
Rhino <no.offline.contact.please(a)example.com> wrote:

> I'm trying to think of a good design for a hypothetical validation of a
> date and would appreciate some suggestions.
>
> Suppose I have a form and one of the things that needs to be entered on the
> form is a date. Let's say that I want the date to look like this: YYYY-MM-
> DD, where the year is in the range 1 to 9999, the month is in the range 1
> to 12, and the day is in the range 1 to 28/29/30/31, depending on what
> month it is and whether it is a leap year. (I've already written and tested
> all the if statements and/or REGEXs that will verify that all parts of the
> date are valid so I don't need any help with that part.)
>
> I'd like the form validation to return a clear and specific error message
> if the date fails to meet any of those conditions so that the user knows
> exactly what to correct. For example, "the day portion of the date can't be
> 29 if the month portion is February and the year is not a leap year". A
> generic message that simply says "invalid date" for all possible errors in
> the date field just doesn't cut it for me.
>
> My current design, recognizing that the date might be find or might be
> invalid, returns TWO items in a two-element Object array: first, a boolean
> that indicates whether the date was valid and second, if there is an error
> in the date, a specific error message.
>
> This code works fine but it feels wrong to return two items from a method.
> I suppose I could just return a message string and, if the message string
> is blank, let that be the way the calling program knows that the date was
> valid. Or would it be better to have the date validation method throw
> exceptions containing the specific error message and then display the error
> message in the program that calls the validation message? That's the
> approach I'm leaning toward.

Why reinvent the wheel? Validation of dates is such a common thing that
it's doubtless been done tons of times and you can find existing code to
handle it. For my work project, we chose to use JCalendar, which is
open source and uses LGPL. It provides graphical controls for entering
the dates. See <http://www.toedter.com/en/jcalendar/> or search for
others and maybe save yourself the grief of implementing it at all.
--
Steve W. Jackson
Montgomery, Alabama
From: markspace on

new JFormattedTextField( new SimpleDateFormat("mm/dd/yy"));

You can also use the InputVerifier class to get a more general range of
behaviors:

JTextField field = new JTextField();
field.setInputVerifier( new InputVerifier() { ...

Neither does exactly what you want, but they're standard in the API and
you might be able to adapt them to your current situation. If you're
using something like JSF for web apps, that's a little different, but
JSF still has standard validation methods.
From: bugbear on
Rhino wrote:
> I'm trying to think of a good design for a hypothetical validation of a
> date and would appreciate some suggestions.
>
> Suppose I have a form and one of the things that needs to be entered on the
> form is a date. Let's say that I want the date to look like this: YYYY-MM-
> DD, where the year is in the range 1 to 9999, the month is in the range 1
> to 12, and the day is in the range 1 to 28/29/30/31, depending on what
> month it is and whether it is a leap year. (I've already written and tested
> all the if statements and/or REGEXs that will verify that all parts of the
> date are valid so I don't need any help with that part.)
>
> I'd like the form validation to return a clear and specific error message
> if the date fails to meet any of those conditions so that the user knows
> exactly what to correct. For example, "the day portion of the date can't be
> 29 if the month portion is February and the year is not a leap year". A
> generic message that simply says "invalid date" for all possible errors in
> the date field just doesn't cut it for me.
>
> My current design, recognizing that the date might be find or might be
> invalid, returns TWO items in a two-element Object array: first, a boolean
> that indicates whether the date was valid and second, if there is an error
> in the date, a specific error message.
>
> This code works fine but it feels wrong to return two items from a method.
> I suppose I could just return a message string and, if the message string
> is blank, let that be the way the calling program knows that the date was
> valid. Or would it be better to have the date validation method throw
> exceptions containing the specific error message and then display the error
> message in the program that calls the validation message? That's the
> approach I'm leaning toward.

I'd have said this is EXACTLY what exceptions are for.

BugBear
From: Martin Gregorie on
On Thu, 18 Mar 2010 14:40:02 +0000, Rhino wrote:

> I'm trying to think of a good design for a hypothetical validation of a
> date and would appreciate some suggestions.
>
> Suppose I have a form and one of the things that needs to be entered on
> the form is a date. Let's say that I want the date to look like this:
> YYYY-MM- DD, where the year is in the range 1 to 9999, the month is in
> the range 1 to 12, and the day is in the range 1 to 28/29/30/31,
> depending on what month it is and whether it is a leap year. (I've
> already written and tested all the if statements and/or REGEXs that will
> verify that all parts of the date are valid so I don't need any help
> with that part.)
>
> I'd like the form validation to return a clear and specific error
> message if the date fails to meet any of those conditions so that the
> user knows exactly what to correct. For example, "the day portion of the
> date can't be 29 if the month portion is February and the year is not a
> leap year". A generic message that simply says "invalid date" for all
> possible errors in the date field just doesn't cut it for me.
>
> My current design, recognizing that the date might be find or might be
> invalid, returns TWO items in a two-element Object array: first, a
> boolean that indicates whether the date was valid and second, if there
> is an error in the date, a specific error message.
>
> This code works fine but it feels wrong to return two items from a
> method. I suppose I could just return a message string and, if the
> message string is blank, let that be the way the calling program knows
> that the date was valid. Or would it be better to have the date
> validation method throw exceptions containing the specific error message
> and then display the error message in the program that calls the
> validation message? That's the approach I'm leaning toward.
>
I normally return a boolean to signal valid/invalid and use a
public String getError() method to return the error text.


--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |