R
Rhino
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.
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.