orpqK
orpqK

Reputation: 2785

smartGWT date input validator

I'm using DateTimeItem for my start and end dates. I don't want the user to be able to select invalid dates, like 13/44/2014:

First image is a example of valid date, the second (one below) is a invalid date.

enter image description here

How do I avoid the second one from happening?

Upvotes: 1

Views: 1893

Answers (2)

Braj
Braj

Reputation: 46841

Try with CustomValidator.

Sample code:

final DateTimeItem dateTimeItem = new DateTimeItem();
final DateTimeFormat dateTimeFormat = DateTimeFormat.getFormat("MM/dd/yyyy");
final DateDisplayFormat dateDisplayFormat = DateDisplayFormat.TOUSSHORTDATE;

dateTimeItem.setDateFormatter(dateDisplayFormat);
dateTimeItem.setTitle("Date");
CustomValidator dateValidator = new CustomValidator() {

    @Override
    protected boolean condition(Object value) {
        try {
            // An exception is throws if an invalid date is entered
            dateTimeFormat.format((Date) value);
        } catch (Exception e) {                
            return false;
        }
        return true;
    }

};
dateValidator.setErrorMessage("Invalid date");
dateTimeItem.setValidators(dateValidator);
dateTimeItem.setValidateOnChange(true);

snapshot:

enter image description here


Alternatively try with ChangedHandler. Same issue is addressed here on SmartGWT forum.

Sample code:

    final DateTimeItem dateTimeItem = new DateTimeItem();
    dateTimeItem
            .addChangedHandler(new ChangedHandler() {
                @Override
                public void onChanged(ChangedEvent event) {
                    try {
                        // An exception is throws if an invalid date is entered
                        dateTimeItem.getValueAsDate();
                    } catch (Exception e) {
                        SC.say("Invalid Date");
                    }
                }
            });

If you are looking for Date Range then try with DateRangeItem that allows a user to select an absolute or relative range of dates via two RelativeDateItems (if allowRelativeDates is true) or two DateItems.

Sample code:

    DateRangeItem dateRangeItem=new DateRangeItem();
    dateRangeItem.setFromDate(fromDate);
    dateRangeItem.setToDate(toDate);
    dateRangeItem.setValue(new DateRange());

snapshot:

enter image description here

Upvotes: 1

Andrei Volgin
Andrei Volgin

Reputation: 41089

You have to parse the string that a user typed in this field and check if this is a valid date.

For example:

public static Long parseDate(String value) {

    try {
        Date date = DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT).parseStrict(value);
        return date.getTime();
    } catch (IllegalArgumentException e) {
    }

    try {
        Date date = DateTimeFormat.getFormat(PredefinedFormat.DATE_MEDIUM).parseStrict(value);
        return date.getTime();
    } catch (IllegalArgumentException e) {
    }

    try {
        Date date = DateTimeFormat.getFormat(PredefinedFormat.DATE_LONG).parseStrict(value);
        return date.getTime();
    } catch (IllegalArgumentException e) {
    }

    try {
        Date date = DateTimeFormat.getFormat(PredefinedFormat.DATE_FULL).parseStrict(value);
        return date.getTime();
    } catch (IllegalArgumentException e) {
    }

    return null;
}

Upvotes: 0

Related Questions