EGL Development User Group - Group home

New ways for validating input in version 8.0.1.1

  
You may have used the MVC framework to perform validation of input supplied by a user. Starting in Rational Business Developer (RBD) version 8.0.1.1, you can perform view-level validation using the new DojoTextField widget. This type of validation happens as the user types, providing the user with instantaneous feedback. Also, like other input widgets, DojoTextField can be integrated with MVC framework as a view widget. Let’s look at a few examples ...

Screen shot of the DojoTextFieldDemo.egl sample file ...

DojoTextField provides both an inputRequired (boolean) and validators (array of a validator delegates) properties to implement validation. It also provides constraints (of type Dictionary) for user to transfer parameters to the validator functions. Here are more details on both ...

Input required property

The simplest validation is to validate whether a user has supplied a value into a field or not. To enable this, set the boolean inputRequired property to true. For example:
 textField1 DojoTextField {  
marginBottom = 20,
fontSize = 12,
placeHolder = "Enter a string",
textCase = DojoLib.UPPER_CASE,
trim = true,
inputRequiredMessage = "This Field is Required",
inputRequired = true
};
You can specify the message that will appear when the input is empty by setting inputRequiredMessage property (as shown).

Custom validation

You can write your own validator function and set it through validators property to provide customized validation. Here is the signature of the validator function:
 Delegate Validator(input String in, constraints dictionary in) returns(string?) end  
The validator delegate is the same as the validator in MVC framework except that it provides constraints to transfer in the parameters. The return value is a string error message (when the input is invalid) or null (when the input is valid). The constraints is a Dictionary, in which we can define unlimited number of key value pairs. Constraints are transferred into the validator functions, in which we can have different logic for different constraint values.

An example of a custom validator:
 textField2 DojoTextField {  
marginBottom = 20,
promptMessage = "Please enter an integer greater than 100",
constraints = new Dictionary{ min = 100},
validators = [myValidator]
};
function myValidator(input string in,constraints Dictionary in) returns(string?)
try
if(input > constraints.min as int)
return (null);
else
return ("The value is not greater than " + constraints.min);
end
onException(ex AnyException)
return ("The value is not a number");
end
return;
end


Pattern-based validation (using regular expression)

Regular expression is used widely for validating that some input matches a pattern. You can use a predefined validator called DojoLib.VALIDATORS.PatternValidator to perform pattern-based validation:
 function PatternValidator(input String in, pattern String in) returns(boolean);  
The PatternValidator receives the input and a regular expression as the parameters and return a boolean indicating whether the input matches the pattern or not. You can wrap the PatternValidator in a custom validator as follows:
 textField3 DojoTextField {  
marginBottom = 20,
promptMessage = "Please enter a digit",
constraints = new Dictionary{ regExp = "\\d+"},
validators = [myRegExp]
};
function myRegExp(input string in, constraints Dictionary in) returns(string?)
if(constraints.regExp isa string && !DojoLib.VALIDATORS.PatternValidator(input, constraints.regExp))
return ("Not a digit");
else
return (null);
end
end


Validators available in DojoLib

EGL provides some predefined validators in DojoLib to address common validation needs:

ValidatorsUsage
IPValidatorValidates an IP address, supports both IPv4 and IPv6
UrlValidatorValidates an URL string
EmailValidatorValidates an email string
TextValidatorValidates a string of its length, whitespace etc. depends on constraints
RangeValidatorValidates a number is between min and max value
NumberFormatValidatorValidates a number formate is comply with the format provided by constraints

All of these validators have some supported constraints key. For example, IPValidator will read constraints.allowIPv6 to determine if ipv6 address is valid. Please refer to RBD documentation for a full list of supported constraints.
 textField4 DojoTextField {  
marginBottom = 20,
promptMessage = "Please enter an IP address",
constraints = new Dictionary{ allowDottedDecimal = true, allowDecimal = false, allowDottedHex = false,
allowDottedOctal=false,allowHex=false, allowIPv6=true, allowHybrid=false},
validators = [myIPValidator]
};
function myIPValidator(input string in,constraints Dictionary in) returns(string?)
if(DojoLib.VALIDATORS.IPValidator(input, constraints))
return (null);
else
return ("Invalid IP Address");
end
end


Using multiple validators

The validators property for DojoTextField is an array. Thus, it is possible to specify multiple validators for a single widget:
 textField5 DojoTextField {  
marginBottom = 20,
inputRequired = true,
constraints = new Dictionary{ min=20, max = 64, regExp = "\\d+"},
validators = [myRegExp, myRangeValidator]
};
In this example, only a digit between 20 and 64 will pass the validation. The validators will run in the declared order. Also, we can dynamically change the validators and it will work immediately. For example, we can add a validator dynamically:
 textField5.validators ::= myValidator  
Note that the constraints are shared by all validators.

Messages

DojoTextField has 3 kinds of messages: promptMessage, inputRequiredMessage, and error messages returned by validators. All messages are displayed in a tool tip next to the text field. Only one kind of message will appear at a time.
  • promptMessage: if defined, display a hint as to what the user should supply into the field. This message is displayed immediately when the field receives focus. The message disappears when user starts typing.
  • inputRequiredMessage: The message to display if text field is empty, but a value is required.
  • Message returned by validators: The message returned by a validator when the value supplied by the user is invalid.
An example:
The position (relative to the text field) of the message can be changed by setting tooltipPosition propert:
 textField6 DojoTextField{   
promptMessage = "My Date Prompt Message",
inputRequiredMessage = "My Date Missing Message",
inputRequired = true,
tooltipPosition = [DojoLib.TOOLTIP_BEFORE, DojoLib.TOOLTIP_AFTER]
};
The tooltipPostion can be DojoLib.TOOLTIP_BEFORE, DojoLib.TOOLTIP_AFTER, DojoLib.TOOLTIP_ABOVE, DojoLib.TOOLTIP_BELOW. In this example, the tool tip is shown to the left of the DojoTextField if there is room on the left. Note that tooltipPosition only indicates a preferred position. The position may not be as specified if there is no room in the specified position.

See the DojoTextFieldDemo sample file for further examples. We hope you make use of these new validation features of RBD. 

Ji Yong Huang