ignore

Validation

Back to Examples

Strecks simplifies form validation by providing an extensible framework for adding validation annotations. An example of a vanilla Struts form is shown below:

/**
* Plain Struts form with validation code
*/
public class HolidayBookingForm extends ActionForm
{

    private String entryId;

    private String title;

    private String startDate;

    private String days;

    private boolean editMode;

    //form getters and setters omitted

    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
    {

        ActionErrors errors = new ActionErrors();

        // TODO validate days (positive integer), date (format), 
        // and title (not exceeding max length)
        boolean hasError = false;

        if (title == null)
        {
            ActionMessage error = new ActionMessage("Title cannot be null", false);
            errors.add("title", error);
            hasError = true;
        }

        if (days == null)
        {
            ActionMessage error = new ActionMessage("Days cannot be null", false);
            errors.add("days", error);
            hasError = true;
        }
        else
        {
            if (!GenericValidator.isInt(days))
            {
                ActionMessage error = new ActionMessage("Days must be number", false);
                errors.add("days", error);
                hasError = true;
            }
        }

        if (startDate == null)
        {
            ActionMessage error = new ActionMessage("Start date cannot be null", false);
            errors.add("startDate", error);
            hasError = true;
        }
        else
        {

            if (!GenericValidator.isDate(startDate, "yyyy-MM-dd", false))
            {
                ActionMessage error = new ActionMessage(
                	"Start date must be a date in yyyy-MM-dd", false);
                errors.add("startDate", error);
                hasError = true;
            }
        }

        if (!hasError)
            return null;

        return errors;

    }

}

Programmatic validation as above is fairly tedious. An XML-based declarative framework is available from Struts in the form of the Commons Validator. Strecks offers an alternative approach, allowing validations to be placed in annotations within the ActionForm:

/**
* Strecks form with validation annotations
*/
public class HolidayBookingForm extends ValidBindingForm
{

    private String title;

    private String startDate;

    private String days;

    private HolidayBooking booking;


    @ValidateRequired(order = 1, key = "holidaybookingform.title.null")
    public void setTitle(String title)
    {
        this.title = title;
    }

    @ValidateRequired(order = 2, key = "holidaybookingform.start.date.null")
    @ValidateDate(key = "holidaybookingform.start.date.format")
    public void setStartDate(String startDate)
    {
        this.startDate = startDate;
    }

    @ValidateRequired(order = 3, key = "holidaybookingform.days.null")
    @ValidateInteger(key = "holidaybookingform.days.number")
    public void setDays(String days)
    {
        this.days = days;
    }
    
    //form setters with bind and conversion annotations omitted

}
SourceForge.net logo java.net Member logo Copyright © 2005-2007 Realsolve Solutions