Validation

Waffle allows validation of action methods by means of the org.codehaus.waffle.validation.ErrorsContext interface. The mechanism is simple and straightforward: to validate each ActionMethod, the user will need to add a method with the identical signature to the ActionMethod being validated except for the additional first argument of type ErrorsContext. Waffle will inject automatically an instance of ErrorsContext to your method.

Waffle allows the validation methods to be defined in two ways:

  1. In a separate controller validator class, conventially named from the controller
  2. In the same controller class
Note: if the validator class is found, it takes precendence over the validation methods in the controller class.

Let's work through an example. Given the ActionMethod "addToCart" in the ShoppingCartController, the simplest way to add validation is to add a new method with the additional ErrorsContext as the first argument. The ActionMethod ensures that the quantity does not exceed 10, if so a new error message is created and add to the ErrorsContext instance.

Alternatively, Waffle allows for an external validation class following a naming convention. Suppose the ShoppingCartController is registered under the name "shoppingCart":

You can register any POJO you would like as a Validator. The only requirement is that it should be registered with the the conventional suffix Validator (or a different suffix that can be configured via the org.codehaus.waffle.validation.ValidatorConfiguration). In other words the POJO registered under the name "fooValidator" would be the Validator for the controller registered under the name "foo". The Validator class will need to provide a separate method for each ActionMethod requiring validation. The following is an example of such a Validator:

Note: the validator class does not need to extend any custom Waffle classes or interfaces.