JBStrap Validators

Validators

To validate data, the framework comes with pre-defined validator classes. These can determine if an object is valid, meaning that the value and the type of the value are checked. If the specified value(s) are valid, the validator will return with a null value. If it is not valid, the validator will return with (e.g Invalid number). The messages of the errors are in the properties file, so the developer can modify these messager, or translate them.

BooleanValidator

The validator checks Boolean (logic) values. The validator considers a null value as a valid Boolean value. It also considers values valid, that can be interpreted as Boolean values (that is, the value can be converted with the BooleanConverter). If the value is not valid, the validator will return with an error message. The error message corresponds to the validator.booleanValidatorError key.

Validating a value using the BooleanValidator:

DateValidator

The validator checks date values. The validator considers a null value valid. It also considers values valid, that can be converted to Date type, according to the specified pattern. If no pattern was specified, or if the pattern is an empty string, the validator will use the default yyyy-MM-dd format. If the value is not valid, the validator will return with an error message, that corresponds to the a validator.DateValidatorError key.

Validating a value using the DateValidator and specified date format:

FloatValidator

The validator checks float values. The validator considers a null value valid. It also considers values valid that can be converted to Float values. If the specified value is not valid, the validator will return with an error message, that corresponds to the validator.floatValidatorError key.

Validating a value using the FloatValidator:

IntegerValidator

The validator checks integer values. The validator considers a null value valid. It also considers values valid that can be converted to Integer values. If the specified value is not valid, the validator will return with an error message, that corresponds to the validator.integerValidatorError key.

Validating a value using the IntegerValidator:

IntegerRangeValidator

The validator checks Integer range values. The validator considers a null value valid. It also considers values valid that can be converted to Integer values and the converted value is equal to the specified minimum value, or is greater than the minimum value, but less or equal to the maximum value. If the specified value can’t be converted to an Integer, the validator will return with an error message, that corresponds to the a validator.integerValidatorError key. If the specified value can be converted, but the converted value does not fall into the range, it will return with an error message that corresponds to the v alidator.integerRangeValidatorError key

Validating a value using the IntegerRAngeValidator. The minimum and maximum values are also specified (0 and 100):

LongValidator

The validator checks Long values. The validator considers a null value valid. It also consider values valid that can be converted into Long values. If the specified value is not valid, the validator will return with an error message, that corresponds to the validator.longValidatorError key.

Validating a value using the FloatValidator:

NullValidator

The validator checks if the specified value is a null value. Every non-null value is considered valid. If the specified value is null, the validator will return with an error message, that corresponds to the validator.nullValidatorError key.

Validating a value using the NullValidator:

Creating a custom validator

If you need a custom validator, you can create your own custom validator class. You only have to implement the DataValidator interface, that only has one required method implementation. This method is the validateData method, and it requires two parameters. The first one is an Object type parameter, where the value that is to be validated is received by the method. The second parameter is a String type parameter, where you can pass the language code. The language code is used to determine which language variation of the error message is used.

In this example, a validator that checks are record is created. The validator will determine the record valid if:

  • It has an “id” column, which has a value that is not null.
  • It has a “valid” column, that has either ‘Y’ or ‘N’ as its value.
  • It has a “name” column, that has a value that is not null, and contains atleast 5 characters.

There can be other columns in the record, the validator will not check these. The validator will consider null values to be invalid. If the vallidation fails, the validator will return an error message, in the corret language, by using the MessageSourceAPI .

The validator source code:

Related pages