001// Copyright 2005 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry.form.validator;
016
017import java.util.Date;
018
019import org.apache.tapestry.form.IFormComponent;
020import org.apache.tapestry.form.ValidationMessages;
021import org.apache.tapestry.valid.ValidationConstraint;
022import org.apache.tapestry.valid.ValidationStrings;
023import org.apache.tapestry.valid.ValidatorException;
024
025/**
026 * Expects the value to be a {@link Date}, and constrains the date to follow a particular date.
027 * 
028 * @author Howard M. Lewis Ship
029 * @since 4.0
030 */
031
032public class MinDate extends BaseValidator
033{
034    private Date _minDate;
035
036    public MinDate()
037    {
038    }
039
040    public MinDate(String initializer)
041    {
042        super(initializer);
043    }
044
045    public void setMinDate(Date minDate)
046    {
047        _minDate = minDate;
048    }
049
050    public void validate(IFormComponent field, ValidationMessages messages, Object object)
051            throws ValidatorException
052    {
053        Date date = (Date) object;
054
055        if (date.before(_minDate))
056            throw new ValidatorException(buildMessage(messages, field),
057                    ValidationConstraint.TOO_SMALL);
058    }
059
060    private String buildMessage(ValidationMessages messages, IFormComponent field)
061    {
062        return messages.formatValidationMessage(
063                getMessage(),
064                ValidationStrings.DATE_TOO_EARLY,
065                new Object[]
066                { field.getDisplayName(), _minDate });
067    }
068
069}