Validators
This page describes a list of the built-in validators, and also shows how to build custom ones.
AttributePresenceValidator
This class provides a way to detect presence of attributes on an Validatable
. The Validatable
can be a simple object, or an ActiveJDBC Model.
Example, say you have a class:
public class Book extends ValidationSupport {
private String title, isbn, authorFirstName, authorLastName;
public Book() {
validatePresenceOf("title", "authorFirstName");
}//setters and getters below
}
You can check validations on it this way:
Book book = new Book();
setTitle("one flew over the cuckoo's nest");
book.System.out.println(book.isValid()); // will print false because the authorFirstName is not set.
DateValidator
Validates the date format. Class definition:
public class Person extends ValidationSupport {
private String firstName, lastName, dob, email;
public Person(String firstName, String lastName, String dob, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.dob = dob;
validateWith(new DateValidator("dob", "yyyy-MM-dd"));
} }
Example usage:
new Person("John", "Doe", "abx-22-02", null);
Person person = System.out.println(person.isValid()); // will print false because the dob format is invalid
System.out.println(person.errors().get("dob")); // will print: attribute dob does not conform to format: yyyy-MM-dd
EmailValidator
Validates that the email format is valid:
public class Person extends ValidationSupport {
private String firstName, lastName, dob, email;
public Person(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
validatePresenceOf("firstName", "lastName");
validateWith(new EmailValidator("email"));
} }
Usage:
new Person("John", "Doe", "john#doe.com");
Person person = System.out.println(person.isValid()); // will print false because the email format is invalid
System.out.println(person.errors().get("email")); // will print: email has bad format
NumericValidator
Checks that a value is numeric (can be converted to a number).
Definition:
public class Group extends ValidationSupport {
private String size;
public Group(String size) {
this.size = size;
validateNumericalityOf("size");
} }
Usage:
Group group = new Group("1");
System.out.println(group.isValid()); // true
new Group("blah");
group = System.out.println(group.isValid()); // false
// System.out.println(group.errors().get("size")); // prints "value is not a number"
RangeValidator
Definition:
public class Box extends ValidationSupport {
private int width;
public Box(int width) {
this.width = width;
validateWith(new RangeValidator("width", 1, 10));
} }
Usage:
Box box = new Box(3); // valid!
new Box(30); // invalid
box = // System.out.println(box.errors().get("width")); // prints "value should be within limits: > 1 and < 10"
RegexpValidator
public class Blog extends ValidationSupport {
private String title;
public Blog() {
validateWith(new RegexpValidator("title", ".*G.*"));
}public Blog(String title) {
this();
this.title = title;
}public void setTitle(String title) {
this.title = title;
} }
Usage:
new Blog("Helo");
Blog blog = System.out.println(blog.isValid()); // prints false
System.out.println(blog.errors().get("title")); // prints: "value does not match given format");
setTitle("Guns, Germs, and Steel");
blog.System.out.println(blog.isValid()); // prints true
TimestampValidator
Validates presence and correct format of a string.
public class Person extends Model {
static {
//...
validateWith(new TimestampValidator("dob", "yyyy-MM-dd"));
//...
}//...
}
This validator ensures that if an attribute dob
is set with a String that is not following a format, it will fail. The usage is exacly the same as that of any other validator.
Converters
Converters are similar to validators, but they try to change the values they are responsible for during a validation process. For more, see: Converters.
Custom Validators
In case you missed those: Custom Validators
How to comment
The comment section below is to discuss documentation on this page.
If you have an issue, or discover bug, please follow instructions on the Support page