Show a form field error message #965
-
Beta Was this translation helpful? Give feedback.
Answered by
engram-design
Jul 2, 2022
Replies: 1 comment 3 replies
-
It's a bit of a pain with bouncer.js in that you need to register your own custom validator so that it knows about it ahead of time. It's a little more work, but it's not too difficult and is a little more robust. const $form = document.querySelector('.fui-form');
$form.addEventListener('registerFormieValidation', function(e) {
const customRule = {
myCustomRule(field) {
// Only apply this rule for the field we want to target.
// Could be using a data-attribute, CSS selector, whatever you need
if (field.getAttribute('name') != 'fields[emailAddress]') {
return;
}
// Add your logic here, return `true` when there is an error to flag
return true;
},
};
const customMessage = {
myCustomRule(field) {
return 'Sorry, this is incorrect.';
},
};
// Add our custom rules
e.detail.validatorSettings.customValidations = {
...e.detail.validatorSettings.customValidations,
...customRule,
};
// Add our custom messages
e.detail.validatorSettings.messages = {
...e.detail.validatorSettings.messages,
...customMessage,
};
}); We do something very similar for some fields, where they have additional validation logic like a File Upload field. |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
roland-d
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's a bit of a pain with bouncer.js in that you need to register your own custom validator so that it knows about it ahead of time. It's a little more work, but it's not too difficult and is a little more robust.