gform_is_duplicate

Description

This filter is executed during the validation of fields marked with the “No Duplicates” rule. Use this hook to specify a custom duplicate validation logic.

Usage

The following would apply to all forms:

add_filter( 'gform_is_duplicate', 'your_function_name', 10, 4 );

To target a specific form append the form id to the hook name. (Format: gform_is_duplicate_FORMID)

add_filter( 'gform_is_duplicate_5', 'your_function_name', 10, 4 );

Parameters

  • $count integer

    The number of duplicate entries. Filtering this value to 0 means there are no duplicates.

  • $form_id integer

    The current form’s id.

  • $field Field Object

    The current field being validated

  • $value string

    The value of the current field

Examples

1. Check if email already registered

This example checks to see if the email address is already in use. If so, consider the email a duplicate and return a count of 1; otherwise return a count of 0.

add_filter( 'gform_is_duplicate', 'noDuplicateEMails', 10, 4 );
function noDuplicateEMails( $count, $form_id, $field, $value ) {
   if ( $field->type == 'email' && get_user_by( 'email', $value ) ) {
     return 1;
   } else {
     return 0;
   }
}

2. Use GFAPI::count_entries

This example uses the GFAPI::count_entries() method, for form ID 1, to get a count of entries which have a payment_status of Paid and contain the submitted value for field ID 2.

add_filter( 'gform_is_duplicate_1', function ( $count, $form_id, $field, $value ) {
	if ( $field->id == 2 && $count ) {
		$search_criteria = array(
			'status'        => 'active',
			'field_filters' => array(
				array(
					'key'   => 'payment_status',
					'value' => 'Paid',
				),
				array(
					'key'   => '2',
					'value' => $value,
				),
			),
		);

		$count = GFAPI::count_entries( $form_id, $search_criteria );
	}

	return $count;
}, 10, 4 );

Source Code

This filter is located in GFFormsModel::is_duplicate() in forms_model.php