Dienstag, 5. Juli 2011

Some Of My Custom Validators for Wicket

Currently I am having a closer look at the Wicket web framework which has some very nice aspects. One of it is it's validation support.There are plenty of tutorials and howtos on the net about how to build your own validators and how to configure them etc. So it would be mute to write yet another one of those (if you think differently let me know, but I doubt that pretty much ;-)).

Wicket has quite a few built in validators for a decent range of every day tasks that you need in most web applications. Those include length and range validations as well as equality of input fields (very useful for password handling) and much more. But I came across some cases that I found no suitable validators readily available. So I built a few of my own validators and would like to share the implementation of two of them because I think those could be useful for other people too.

The first validator is useful for something like a form to change a users password. In this case you want the user to enter a different password than his existing one. Wicket provides you with the EqualPasswordInputValidator to check if two password fields contain the same text but not for one to check the opposite, so here is my attempt for this task.

package org.butterbrot.rankem.validation;

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;
import org.apache.wicket.util.lang.Objects;

/**
 * Checks for two text components to contain different values.
 * 
 * 
 * @author Chris
 * 
 */
public class UnequalPasswordsValidator extends AbstractFormValidator {

 /**
  * 
  */
 private static final long serialVersionUID = 5677456180685647137L;
 private final FormComponent[] components;

 @SuppressWarnings("unchecked")
 public UnequalPasswordsValidator(FormComponent formComponent1, 
                        FormComponent formComponent2) {
  if (formComponent1 == null) {
   throw new IllegalArgumentException("argument formComponent1 cannot be null");
  }
  if (formComponent2 == null) {
   throw new IllegalArgumentException("argument formComponent2 cannot be null");
  }
  this.components = new FormComponent[] { formComponent1, formComponent2 };
 }

 public FormComponent[] getDependentFormComponents() {
  return this.components;
 }

 public void validate(Form form) {
  final FormComponent formComponent1 = this.components[0];
  final FormComponent formComponent2 = this.components[1];

  if (Objects.equal(formComponent1.getInput(), formComponent2.getInput())) {
   this.error(formComponent2);
  }

 }

}

Another one covers a more specialized use case. Assume you have a form where the user has to enter some numeric values like defining an interval or some kind of slice (maybe a portion of a video or a range) and you want to make sure that two values have a certain difference in their values. This validator allows you to check exactly that.

package org.butterbrot.rankem.validation;

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;

/**
 * Checks if the contents of two number components differ at least the specified
 * difference. If the difference has a positive value the first component must
 * contain the lesser value and vice versa. Since the check internally operates
 * on the double representations of the given values this class should not be
 * used for BigDecimal components.
 * 
 * @author Chris
 * 
 */
public class NumberDifferenceValidator extends AbstractFormValidator {

 /**
  * 
  */
 private static final long serialVersionUID = -1237357103535636603L;
 private final FormComponent[] components;
 private final Number difference;

 @SuppressWarnings("unchecked")
 public NumberDifferenceValidator(FormComponent formComponent1,
   FormComponent formComponent2, Number difference) {
  if (formComponent1 == null) {
   throw new IllegalArgumentException("argument formComponent1 cannot be null");
  }
  if (formComponent2 == null) {
   throw new IllegalArgumentException("argument formComponent2 cannot be null");
  }
  if (difference == null) {
   throw new IllegalArgumentException("argument dfference cannot be null");
  }
  this.difference = difference;
  this.components = new FormComponent[] { formComponent1, formComponent2 };
 }

 public FormComponent[] getDependentFormComponents() {
  return this.components;
 }

 public void validate(Form arg0) {
  if (this.components[1].getConvertedInput().doubleValue() - 
            this.components[0].getConvertedInput().doubleValue() < this.difference
    .doubleValue()) {
   this.error(this.components[0]);
  }

 }

}


Of course I would be grateful for any comments on what you think of these examples or how these could be improved.

Kommentare:

  1. in wich component you put the validator, i mean its
    form.add(validator)?,
    or formcomponent1.add(validator)?

    AntwortenLöschen
  2. Hi,

    as you already suspected those validators are added to the form itself because we want to validate the form and not the content of the formcomponent in it self.

    And thanks for reading ;-)

    AntwortenLöschen