Validating a Date Field with RIFE

June 7, 2006 on 9:28 pm | In Java |

Here is a quick way to validate a multi-part date field in RIFE. By “multi-part field”, I mean one that is made of several fields, in this particular case, a date field, a month field and a year field.

This is best done by creating a bean to hold the input data. Say, we are creating a form to schedule a task. The required input are:

* title
* description
* due date

The TaskBean will be as follows:

public class TaskBean extends Validation {
	private String title;
	private String description;
	private int[] dueDate;

	//... accessor methods

	public void activateValidation() {
		addConstraint(new ConstrainedProperty("title").notNull(true).notEmpty(true));

		addRule(new AbstractValidationRule() {
			public boolean validate() {
				boolean validated = true;

				int day = dueDate[0], month = dueDate[1], year = dueDate[2];

				//... validate field components

				return validated;
			}

			public ValidationError getError() {
				return new ValidationError("INVALID_DATE", "dueDate");
			}
		});
	}
}

The gist of the solution here is the addRule method of Validation subclass. It adds a new AbstractValidationRule that contains a method for validating a given input (in this case, the due date) and returning any found error.

Related Posts:

No Comments yet »

RSS feed for comments on this post.

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>





Powered by blog.mu with Pool theme design by Borja Fernandez.