gform_form_editor_can_field_be_added

Description

This filter fires on the Form Editor page when a field button is clicked or dragged on to the form, it can be used to prevent a field being added to the form if the conditions you define are not met.

Usage

gform.addFilter( 'gform_form_editor_can_field_be_added', function ( canFieldBeAdded, type) {
	// return false to prevent a field being added.
	return canFieldBeAdded;
} );

Parameters

  • canFieldBeAdded boolean

    Defaults to true.

  • type string

    The current field type.

Example

gform.addFilter('gform_form_editor_can_field_be_added', function (canFieldBeAdded, type) {
	if (type !== 'your_custom_type') {
		return canFieldBeAdded;
	}

	if (GetFieldsByType(['product']).length <= 0) {
		alert('You must add a Product field to the form first');
		return false;
	} else if (GetFieldsByType(['your_custom_type']).length) {
		alert('Only one [your field type here] field can be added to the form');
		return false;
	}

	return canFieldBeAdded;
});

Source Code

This filter is located in js.php