Creating Validator
Requires Platform 4.5+
Creating a Validator
You can extend the validator class to create your own validators and validator element tags.
To create your own validator, you must perform the following major steps:
- Create a subclass of the Validator class
- Define a validator element that references your custom Validator class
Subclass
The following example shows a validator subclass:
package myvalidators; public class EmailValidator extends Validator { protected boolean validate (Object objectToValidate) throws ValidationException{ ... } }
Element Tag
Use the validator element to create a custom validator element tag as shown in the following example:
<validator:validator id="emailValidator" class="myvalidators.EmailValidator" value="email.text" onSuccess="" onFailure="macro://failureMacro.execute('Not a valid email address')" />
Creating a Subclass of the Validator Class
To create a validator, you must subclass: com.nexaweb.plugin.validation.validators.Validator and override #validate.
The Validator class contains the following methods that you can use to create a validator:
Method | Description | Required/Optional |
initialize(ClientSession, Element) | Initializes the validator with the client session and element object associated with this validator. Called before validation occurs. Throws ValidatorInitializationException. Makes user input in field that you want to validate available to an MCO or Macro that provides validation onSuccess or onFailure messages. | Optional |
validate (Object) | Performs the actual validation on the specified object. Throws ValidationException. | Required |
For example:
package myvalidators.HairColorValidator; public class HairColorValidator extends Validator { protected boolean validate (Object objectToValidate) { List hairColors = new ArrayList(); hairColors.add("brown"); hairColors.add("red"); hairColors.add("black"); return hairColors.contains(objectToValidate); } }
Defining a Validator Element
Create an element tag to use a validator that you created in a Nexaweb client application.
Use the <val:validator /> element to create an tag for a custom validator as you would for a supplied validator and include the following attributes:
Attribute | Description |
xmlns:val=“http://nexaweb.com/validator | Specify the validator namespace. |
class= | References the validator subclass that you created for this custom validator. |
Example:
<val:validator xmlns:val="http://nexaweb.com/validator" id="hairColorValidator" value="hairColor.text" class="myvalidators.HairColorValidator" onSuccess="mco://myMco.validHairColor()" onFailure="mco://myMco.invalidHairColor()"/>