Custom Validators are very easy to create for specific requirements, for example an organisation may have a specific need for creating rules that ensure proper formating of serial numbers, product codes, or bar code conventions.
A developer can create a new validator and add this to the OpenLinks Validator Library and then reference this Custom Validator in the Open Links Service Configuration File, an example is shown of a validator below.
package webdirector.validation.rules;
import java.util.HashMap;
import webdirector.validation.Rule;
public class EmailRule implements Rule {
String fieldName;
HashMap<String, String> errorMsgs = new HashMap();
public EmailRule( String fieldName ) {
this.fieldName = fieldName;
errorMsgs.put( fieldName, "Not a valid email" );
}
public boolean checkRule( HashMap<String, String> formResults ) {
String input = formResults.get( fieldName );
if ( input == null || input.equals( "" ) || input.matches( "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}$" ) )
return true;
return false;
}
public HashMap<String, String> getErrorMessages() {
return errorMsgs;
}
public boolean checkRule( String value ) {
HashMap<String, String> result = new HashMap<String, String>();
result.put( fieldName, value );
return checkRule( result );
}
public String getErrorMessage() {
HashMap<String, String> errors = getErrorMessages();
return errors.get( fieldName );
}
}