For the Frontend i would like to have a simple Form Validation (client side first) including a dedicated error message. The Field validation has been written and its working:
document.addEventListener('DOMContentLoaded', function() {
console.log('client-validation-age.js loaded');
'use strict';
if(document.formvalidator){
console.log('client-validation-age.js active');
document.formvalidator.setHandler('age', function(value) {
const currentYear = new Date().getFullYear();
const birthYear = new Date(value).getFullYear();
const age = currentYear - birthYear;
console.log('age', age);
return age >= 5;
});
}
})
I've found this (I think outdated) answer here: How can I provide a custom message for a failed JFormRule validation?
From my understanding the response message should be placed into the JS code because its client side. How to return the error message too? I've already tried to return an object with the keys valid and message like so:
// Not working:
...
if (age < 5) {
return {
valid: false,
message: 'Age must be at least 5 years.'
};
}
...
Is it / how is it possible to set an individual message in the JS for client-side validation?