Design of date validation method

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.
 
M

markspace

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.
 
M

Martin Gregorie

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

Roedy Green

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

Have a look at BigDate. It is step toward what you want.

See
https://wush.net/websvn/mindprod/fi...prod&path=/com/mindprod/common11/BigDate.java
--
Roedy Green Canadian Mind Products
http://mindprod.com

Responsible Development is the style of development I aspire to now. It can be summarized by answering the question, “How would I develop if it were my money?” I’m amazed how many theoretical arguments evaporate when faced with this question.
~ Kent Beck (born: 1961 age: 49) , evangelist for extreme programming.
 
J

John B. Matthews

markspace said:
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.

Here's an example that supports multiple formats:
 
D

Daniel Pitts

I'm trying to think of a good design for a hypothetical validation of a
date and would appreciate some suggestions.
You might want to look into Spring Binding. Its part of the Spring
Framework.

They have a very decent validation framework.

The approach that they take is that there is a Validator class which
will register problems with an Errors object. The errors object then
will have a list of errors (global and/or field specific) that can be
presented to the user.

It also handles "conversion" errors. If you have a form submission
that has string values, but your backing bean has Dates, Integers, or
Frobitzs, etc, it will try to convert them. If any conversion fails
(throws an exception for instance), then the binder will add that to the
Errors object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top