All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.wicket.validation.validator.DateValidator Maven / Gradle / Ivy

Go to download

Wicket is a Java web application framework that takes simplicity, separation of concerns and ease of development to a whole new level. Wicket pages can be mocked up, previewed and later revised using standard WYSIWYG HTML design tools. Dynamic content processing and form handling is all handled in Java code using a first-class component model backed by POJO data beans that can easily be persisted using your favorite technology.

There is a newer version: 10.0.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.wicket.validation.validator;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.wicket.validation.IValidatable;
import org.apache.wicket.validation.ValidationError;

/**
 * Validator for checking if a given date falls within [min,max] range.
 * 
 * If either min or max are {@code null} they are not checked.
 * 
 * 

* Resource keys: *

    *
  • {@code DateValidator.exact} if min==max
  • *
  • {@code DateValidator.range} if both min and max are not {@code null}
  • *
  • {@code DateValidator.minimum} if max is {@code null}
  • *
  • {@code DateValidator.maximum} if min is {@code null}
  • *
*

* *

* Error Message Variables: *

    *
  • {@code name}: the id of {@code Component} that failed
  • *
  • {@code label}: the label of the {@code Component} (either comes from * {@code FormComponent.labelModel} or resource key {@code .}
  • *
  • {@code input}: the input value
  • *
  • {@code inputdate}: the formatted input value
  • *
  • {@code minimum}: the minimum allowed value
  • *
  • {@code maximum}: the maximum allowed value
  • *
*

* * @author igor */ public class DateValidator extends RangeValidator { private static final long serialVersionUID = 1L; /** * @param minimum * the minimum Date * @param maximum * the maximum Date * @return a {@link DateValidator} that validates if a date is between (inclusive) a minimum and * maximum */ public static DateValidator range(Date minimum, Date maximum) { return new DateValidator(minimum, maximum); } /** * @param minimum * the minimum Date * @param maximum * the maximum Date * @param format * The format string used to format the date with SimpleDateFormat * * @return a {@link DateValidator} that validates if a date is between (inclusive) a minimum and * maximum */ public static DateValidator range(Date minimum, Date maximum, String format) { return new DateValidator(minimum, maximum, format); } /** * @param minimum * the minimum Date * * @return a {@link DateValidator} that validates if a date is after or equal to a minimum date */ public static DateValidator minimum(Date minimum) { return new DateValidator(minimum, null); } /** * @param minimum * the minimum Date * @param format * The format string used to format the date with SimpleDateFormat * * @return a {@link DateValidator} that validates if a date is after or equal to a minimum date */ public static DateValidator minimum(Date minimum, String format) { return new DateValidator(minimum, null, format); } /** * @param maximum * the maximum Date * * @return a {@link DateValidator} that validates if a date is before or equal to a maximum date */ public static DateValidator maximum(Date maximum) { return new DateValidator(null, maximum); } /** * @param maximum * the maximum Date * @param format * The format string used to format the date with SimpleDateFormat * * @return a {@link DateValidator} that validates if a date is before or equal to a maximum date */ public static DateValidator maximum(Date maximum, String format) { return new DateValidator(null, maximum, format); } private String format; /** * Constructor that sets the minimum and maximum date values and a custom date formating. * * @param minimum * the minimum date * @param maximum * the maximum date * @param format * The format string used to format the date with SimpleDateFormat */ public DateValidator(Date minimum, Date maximum, String format) { super(minimum, maximum); this.format = format; } /** * Constructor that sets the minimum and maximum date values. * * @param minimum * the minimum date * @param maximum * the maximum date */ public DateValidator(Date minimum, Date maximum) { this(minimum, maximum, null); } /** * Constructor used for subclasses who want to set the range using * {@link #setRange(Comparable, Comparable)} */ protected DateValidator() { } @Override protected ValidationError decorate(ValidationError error, IValidatable validatable) { error = super.decorate(error, validatable); error.setVariable("inputdate", validatable.getValue()); // format variables if format has been specified if (format != null) { SimpleDateFormat sdf = new SimpleDateFormat(format); if (getMinimum() != null) { error.setVariable("minimum", sdf.format(getMinimum())); } if (getMaximum() != null) { error.setVariable("maximum", sdf.format(getMaximum())); } error.setVariable("inputdate", sdf.format(validatable.getValue())); } return error; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy