Introduction
The gform/gfcf/visibility_field_classes filter allows customization of which field CSS classes should be monitored for visibility changes. The field observer system uses it to watch for changes to the style.display property of fields.
Usage
addFilter( 'gform/gfcf/visibility_field_classes', ( fieldClasses ) => {
// Custom logic to modify fieldClasses
return fieldClasses;
} );
Use Cases
- Monitoring fields that JavaScript dynamically hides.
- Integrating with third-party add-ons that modify field visibility
Parameters
| Parameter | Type | Description |
|---|---|---|
| fieldClassesToObserve | string[] | Default array of CSS class names for fields that the visibility observer should monitor. It can be modified by the filter to add or remove classes. When a field’s display property changes to none, the field will be hidden in the conversational form. |
By default, the filter monitors these classes:
gfield--type-stripe_creditcardgfield--type-paypalgfield--type-square_creditcardgfield--type-molliegfield--type-2checkout_creditcardgfcf-observe-visibility
Examples
Add custom classes to monitor.
// Add custom classes to monitor
addFilter('gform/gfcf/visibility_field_classes', function(classes) {
// Add a class for custom payment fields
classes.push('gfield--type-custom-payment');
// Add a class for fields that should be monitored
classes.push('my-custom-visible-field');
// Remove a default class if you don't want to monitor it
const index = classes.indexOf('gfield--type-paypal');
if (index > -1) {
classes.splice(index, 1);
}
return classes;
});
Since
This filter was added in Conversational Forms 1.7.0
Source Code
This filter is located in assets/js/src/theme/components/form/field-display-observer.js
Notes
- The
gfcf-observe-visibilityclass can be added to any field to make it observable without using the filter. - The field observer uses MutationObserver to watch for changes to the
styleattribute. - Only changes to the
displayproperty are considered.