When users manually type a username into a Joget form field—such as assigning a person in charge, manager, or auditor—standard text inputs do not check if that username actually exists in Joget Directory Manager. If a user mistypes a username, the workflow will throw an error when trying to assign the subsequent activity.
In this guide, we'll write a custom BeanShell Form Validator script that checks dir_user in real time during form submission and displays an inline validation error if the entered username does not exist.
How It Works
-
Interpreting Input Values: The validator signature
validate(Element element, FormData formData, String[] values)receives the user's input from the form field. -
Directory Lookup: A parameterized SQL query (
SELECT COUNT(*) FROM dir_user WHERE username = ?) checks the Joget Directory user database. -
Form Error Injection: If the count is zero,
formData.addFormError(elementId, errorMessage)marks the field as invalid and stops form submission.
The BeanShell Form Validator Script
Open your Joget Form in Form Builder, select the target Username field, set Validator to BeanShell Validator, and paste the script below:
import org.joget.apps.form.model.Element;
import org.joget.apps.form.model.FormData;
import org.joget.apps.form.service.FormUtil;
import org.joget.commons.util.LogUtil;
import org.joget.apps.app.service.AppUtil;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public boolean validateUsername(Element element, FormData formData, String[] values) {
String elementId = FormUtil.getElementParameterName(element);
// 1. Check for empty input
if (values == null || values.length == 0 || values[0] == null || values[0].trim().isEmpty()) {
formData.addFormError(elementId, "Username is required.");
return false;
}
String inputUsername = values[0].trim();
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
boolean userFound = false;
try {
// 2. Obtain DataSource and check Directory user table
DataSource ds = (DataSource) AppUtil.getApplicationContext().getBean("setupDataSource");
con = ds.getConnection();
if (con != null && !con.isClosed()) {
String sql = "SELECT COUNT(*) AS user_count FROM dir_user WHERE username = ?";
stmt = con.prepareStatement(sql);
stmt.setString(1, inputUsername);
rs = stmt.executeQuery();
if (rs.next() && rs.getInt("user_count") > 0) {
userFound = true;
}
}
// 3. Highlight field with inline error if user does not exist
if (!userFound) {
formData.addFormError(elementId, "User '" + inputUsername + "' does not exist in the system directory.");
}
} catch (Exception e) {
LogUtil.error("Directory User Validator", e, "Database error during username validation");
return false;
} finally {
if (rs != null) try { rs.close(); } catch (Exception e) {}
if (stmt != null) try { stmt.close(); } catch (Exception e) {}
if (con != null) try { con.close(); } catch (Exception e) {}
}
return userFound;
}
// Execute validation
return validateUsername(element, formData, values);
Alternative: Using DirectoryManager API
You can also perform the user lookup using Joget's DirectoryManager API directly:
import org.joget.apps.app.service.AppUtil;
import org.joget.directory.model.User;
import org.joget.directory.model.service.DirectoryManager;
DirectoryManager dirManager = (DirectoryManager) AppUtil.getApplicationContext().getBean("directoryManager");
User user = dirManager.getUserByUsername(inputUsername);
if (user == null) {
formData.addFormError(elementId, "Specified user was not found.");
return false;
}
return true;
Pro Tip
Use the direct SQL validator for high-speed bulk form submissions, and dirManager.getUserByUsername() when you also need to check whether the user is currently active (user.getGroupList(), user.getRoleList()).
Top comments (0)