You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by T Ames <ta...@gmail.com> on 2009/08/12 16:59:13 UTC

Date Validation - From - To Dates

I have coded a simple Validator below that may help others out who are
looking for a way to make sure that a from-date and a to-date on a form fall
within a proper range of each other.  Meaning that a from date cannot be
greater than a to date.

I did this based on an older post of the same subject to this list where
Igor (Thanks!) pointed us to look at EqualInputValidator as an example. So
with that in mind I copied EqualInputValidator and modified it.

It is called DateDependencyInputValidator - I could not come up with a
better name :)

I am not a great Java coder, but you can take this and do whatever you
want.  IMHO it would be great to put this idea into the Wicket core since
this is a very common issue much like EqualInputValidator.

The error message I used looks like this:
DateDependencyInputValidator='${input0}' from ${label0} and '${input1}' from
${label1} are not in a correct range.

------------------------------------------------------------------------------------------------------------------------------------------------------------


import java.util.Date;

import org.apache.wicket.datetime.markup.html.form.DateTextField;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.validation.AbstractFormValidator;

/**
 * Validates that the input of two date form components are within range of
each
 * other. The FROM date cannot be greater than the TO date and the TO date
 * cannot be less than the FROM date. An argument is passed as to whether
 * you want to allow the dates to be equal.
 *
 * Errors are reported on the second form component with key
 * 'DateDependencyInputValidator' and the variables:
 * <ul>
 * <li>${input(n)}: the user's input: n=0 fromDate, n=1 toDate</li>
 * <li>${name}: the name of the component</li>
 * <li>${label(n)}: the label of the component - either comes from
 * FormComponent.labelModel or resource key [form-id].[form-component-id] in
 * that order</li>
 * </ul>
 *
 * To use this add it to the form itself: add(new
 * DateDependencyInputValidator(date1, date2, true)
 *
 * In your page/panel .properties file do something like this:
 * DateDependencyInputValidator='${input0}' from ${label0} and '${input1}'
from ${label1} are not in a correct range.
 *
 * This was copied and modified from the Wicket EqualInputValidator - Igor
Vaynberg (ivaynberg)
 *
 * <p>
 * <b>Versions</b>
 * <ul>
 * <li>08/11/2009</li>
 * </ul>
 * </p>
 *
 *
 * @author T.Ames
 */
public class DateDependencyInputValidator extends AbstractFormValidator {

    private static final long serialVersionUID = 1L;

    private boolean equalAllowed;
    /** form components to be checked. */
    private final FormComponent<?>[] components;


    /**
     * Construct.
     *
     * @param fromDateComponent
     *            a DateTextField
     * @param toDateComponenet
     *            a DateTextField
     * @param allowEqual
     *             true = allow dates to be equal
     *             false = the dates cannot be equal
     */
    public DateDependencyInputValidator(DateTextField fromDateComponent,
DateTextField toDateComponent, boolean equalAllowed) {

        if (fromDateComponent == null) {
            throw new IllegalArgumentException("argument fromDateComponent
cannot be null");
        }
        if (toDateComponent == null) {
            throw new IllegalArgumentException("argument toDateComponent
cannot be null");
        }
        components = new FormComponent[] { fromDateComponent,
toDateComponent };
        this.equalAllowed = equalAllowed;
    }



    /**
     * @see
org.apache.wicket.markup.html.form.validation.IFormValidator#getDependentFormComponents()
     */
    public FormComponent<?>[] getDependentFormComponents() {
        return components;
    }



    /**
     * @see
org.apache.wicket.markup.html.form.validation.IFormValidator#validate(org.apache.wicket.markup.html.form.Form)
     */
    public void validate(Form<?> form) {

        Date fromDate;
        Date toDate;


        final DateTextField fromDateComponent = (DateTextField)
components[0];
        final DateTextField toDateComponent = (DateTextField) components[1];

        fromDate = fromDateComponent.getConvertedInput();
        toDate = toDateComponent.getConvertedInput();

        // Compare the dates.
        // If the dates are equal and we are not allowing them to be
        // OR
        // The begin date is greater than the end date
        int compDate = fromDate.compareTo(toDate);
        if ( (compDate == 0 && ! equalAllowed) || compDate > 0) {
            error(toDateComponent);
        }

    }
}