This repository provides a simple yet effective validation structure for Servoy projects. It includes helper functions to validate required fields on a form, display error labels for missing values, show error dialogs, and hide error labels when needed. The validation logic is designed to be placed in a Servoy scope (e.g., the globals scope) for easy reuse across forms. π
- β Checks if specified fields in a foundset are empty.
- π·οΈ Displays custom error labels (e.g.,
fieldName_ErrorLabel) next to empty fields. β οΈ Shows a popup dialog with aggregated error messages.- β Hides error labels on cancel or reset actions.
- π Supports custom foundsets for flexibility.
- Create a new scope in your Servoy solution (e.g.,
globals) or use an existing one. - Copy the validation functions (
validateFieldsandhideErrorLabels) into a JavaScript file in that scope (e.g.,validation.js). - In your forms, ensure that for each required field (e.g.,
customerid), you have a corresponding error label element namedcustomerid_ErrorLabel(hidden by default, with text like "This field is required"). - Call the functions from your form events, passing the required fields array, form name, and optionally a custom foundset.
No additional dependencies are required, as this uses built-in Servoy APIs like forms, databaseManager, plugins.dialogs, and application.output. π¦
To use this validation in your Servoy forms, define an array of required field names at the top of your form's JavaScript file. Then, call globals.validateFields in save events to check for errors before saving data. Use globals.hideErrorLabels in cancel or reset events to clear the error indicators.
Here's how to integrate it into a form like order_edit.js:
// order_edit.js
// Define the required fields at the top for easy maintenance
var requiredFields = ['customerid', 'orderdate', 'shipcountry', 'shipcity', 'shipaddress', 'shippostalcode', 'shipregion', 'shipvia'];
/**
* Save the order after validating required fields.
* @param {JSEvent} event - The event triggering the save.
*/
function saveOrder(event) {
// Validate fields using the globals function
// Pass requiredFields, the current form name, and the foundset
var hasError = globals.validateFields(requiredFields, controller.getName(), foundset);
if (!hasError) {
// Proceed with saving if no errors
databaseManager.saveData(foundset);
application.output('Main foundset is saved!');
}
// If errors, the function will handle showing labels and dialog
}
/**
* Cancel the order and hide any error labels.
* @param {JSEvent} event - The event triggering the cancel.
*/
function cancelOrder(event) {
// Hide error labels using the globals function
globals.hideErrorLabels(requiredFields, controller.getName());
}- requiredFields: An array of dataprovider names (field names) that must not be empty. π
- validateFields(requiredFields, formName, customFoundset):
- Iterates through each required field.
- Checks if the value in the foundset is empty (
!value). - If empty, shows the corresponding
_ErrorLabelelement and collects error messages. - If errors are found, displays a dialog with all messages joined by line breaks.
- Returns
trueif there are errors,falseotherwise.
- hideErrorLabels(requiredFields, formName):
- Hides all
_ErrorLabelelements for the required fields.
- Hides all
- Customization π§:
- Error labels must exist on the form and be named consistently (e.g.,
shipcountry_ErrorLabel). - Default error message: "Fields are can not be empty" (customize via label text).
- Use a custom foundset if validating a different record set.
- Error labels must exist on the form and be named consistently (e.g.,
Copy the validation functions (validateFields and hideErrorLabels) from the globals.js file in this repository and paste them into your own scopes/globals.js file in your Servoy solution.
Feel free to fork this repo and submit pull requests for improvements, such as adding more validation types (e.g., regex checks) or better error handling. Your contributions are welcome! π
This project is licensed under the MIT License. See the LICENSE file for details. (Create a LICENSE file in your repo if needed.)