WebMaker provides inbuilt support for validating the data entered on a page, and can display error messages against each field as needed.
There are a default set of error message strings provided, but it is possible to customize these as required by adding some additional script to your page. The approach detailed below requires WebMaker version 3.1.1 or later.
Please note, that when adding additional JavaScript code to a project, this should always be placed in a separate script file rather than in one of the ones provided for each project. This keeps the separation between your new code, and the functionality provided by WebMaker, and also reduces the chance of issues when upgrading to a newer WebMaker version. To do this you can use the controls on the Application Map tab within the studio to create a new script file for your application or for a specific page as appropriate.
To ensure that your messages are used instead of the default ones, then you need to define a new function that will produce the required error messages, and then make the validation process aware of it.
This new message function will be called whenever an error message is needed, and will be passed two parameters, the HTML field (eg a text box) that is in error, and a code indicating the type of error that the field has.
When registering your error message function you can make it apply to all types of error, or restrict it to only be called to generate messages for errors of a specific type.
Multiple error message functions can be added (even for the same error code), and they will be called in the order in which they were added. Functions that are only interested in a specific error type will be called before those handling all error types. The first function that returns a valid message string will be used as the message displayed to the user. If none of the called functions returned a message (or if there are no functions defined for the current error code) then the default message will be used instead.
The list of available error codes can be found within the ValidationError.js script file, and the default messages that will be displayed are listed at the end of the DisplayMessages.js file. As mentioned however, please avoid making changes to these files.
The following script fragments hopefully illustrates how functions can be created and registered to produce specific error messages
There are a default set of error message strings provided, but it is possible to customize these as required by adding some additional script to your page. The approach detailed below requires WebMaker version 3.1.1 or later.
Please note, that when adding additional JavaScript code to a project, this should always be placed in a separate script file rather than in one of the ones provided for each project. This keeps the separation between your new code, and the functionality provided by WebMaker, and also reduces the chance of issues when upgrading to a newer WebMaker version. To do this you can use the controls on the Application Map tab within the studio to create a new script file for your application or for a specific page as appropriate.
To ensure that your messages are used instead of the default ones, then you need to define a new function that will produce the required error messages, and then make the validation process aware of it.
This new message function will be called whenever an error message is needed, and will be passed two parameters, the HTML field (eg a text box) that is in error, and a code indicating the type of error that the field has.
When registering your error message function you can make it apply to all types of error, or restrict it to only be called to generate messages for errors of a specific type.
Multiple error message functions can be added (even for the same error code), and they will be called in the order in which they were added. Functions that are only interested in a specific error type will be called before those handling all error types. The first function that returns a valid message string will be used as the message displayed to the user. If none of the called functions returned a message (or if there are no functions defined for the current error code) then the default message will be used instead.
The list of available error codes can be found within the ValidationError.js script file, and the default messages that will be displayed are listed at the end of the DisplayMessages.js file. As mentioned however, please avoid making changes to these files.
The following script fragments hopefully illustrates how functions can be created and registered to produce specific error messages
/** Example 1: Provide a custom error message for a specific field
* for the error situation that a required value has not been entered. */
/* Create a function to check the field and return the new error message
* if it is the correct field. */
function myMessageFunc(field, errorCode)
{
if (field.getAttribute('id') == 'my_field')
return 'This is a custom error message';
}
/* Register this function to only be called for mandatory value errors.*/
hyf.validation.DisplayMessages.addMessageProducer(myMessageFunc,
hyf.validation.ValidationError.ERROR_REQUIRED);
/** Example 2: Provide custom error messages for particular types of
* error, regardless of which field has the error. */
/* Create a function that checks the supplied errorCode
* to determine whether or not to return an error message. */
function anotherMessageFunc(field, errorCode)
{
if (errorCode == hyf.validation.ValidationError.ERROR_REQUIRED)
return 'This field is mandatory, but you have not provided a value';
else if (errorCode == hyf.validation.ValidationError.ERROR_NAN)
return 'This value must be a number';
}
/* Register this function to be called for any type of error. */
hyf.validation.DisplayMessages.addMessageProducer(anotherMessageFunc);