WebMaker provides support for automatic validation of fields by simply entering the data constraints on the 'Properties' tab within Page Design.
In some cases though, you may have additional or more complex validation rules that need to be enforced, and so WebMaker provides the ability to add your own validation checks, but still display the errors in the standard way.
You would need to create your own script function to first determine if there is an error. If there is, you would then create a ValidationError object describing the error, and pass this to the ErrorDisplay object.
The ValidationError object needs to be given the HTML field that the error should be associated with, plus the error code indicating the type of error. There are a number of existing error codes that can be used, as listed in the ValidationError.js script file, or you can setup a new one. If you do use a new error code, it is important to also specify what error message should be shown for the error. Please see How do I customize the client side validation messages displayed on a page? for more details.
The following script fragment illustrates the approach needed to create and display a custom error.
This will cause the error to be displayed the next time the validation process is run. You will want to add a call to this function from your button/control using the Events tab on Page Design. You should put the call to your new function before the form submission or ajax submission action. In this way your custom error will be shown at the same time as any standard errors, and will prevent the data being submitted when it is present.
If you would like to see the error displayed straight away, rather than linked to a submission event, please see How do I invoke the validation routines from my custom JavaScript function?
In some cases though, you may have additional or more complex validation rules that need to be enforced, and so WebMaker provides the ability to add your own validation checks, but still display the errors in the standard way.
You would need to create your own script function to first determine if there is an error. If there is, you would then create a ValidationError object describing the error, and pass this to the ErrorDisplay object.
The ValidationError object needs to be given the HTML field that the error should be associated with, plus the error code indicating the type of error. There are a number of existing error codes that can be used, as listed in the ValidationError.js script file, or you can setup a new one. If you do use a new error code, it is important to also specify what error message should be shown for the error. Please see How do I customize the client side validation messages displayed on a page? for more details.
The following script fragment illustrates the approach needed to create and display a custom error.
function extraValidation()
{
var fieldToCheck = document.getElementById('myfield');
//check if there is an error
if (....)
{
//create the ValidationError object
var errorCode = hyf.validation.ValidationError.ERROR_REQUIRED;
//or
var errorCode = 101;
/*if creating new error codes, use 3 digit numbers to avoid potential
conflicts with built in errors. */
var newError = new hyf.validation.ValidationError(fieldToCheck, errorCode);
//find the ErrorDisplay to add the error to
var errorDisplay = hyf.FMAction.getErrorDisplay();
errorDisplay.addError(newError);
}
}
This will cause the error to be displayed the next time the validation process is run. You will want to add a call to this function from your button/control using the Events tab on Page Design. You should put the call to your new function before the form submission or ajax submission action. In this way your custom error will be shown at the same time as any standard errors, and will prevent the data being submitted when it is present.
If you would like to see the error displayed straight away, rather than linked to a submission event, please see How do I invoke the validation routines from my custom JavaScript function?