Using Gravity Forms ‘gform_rule_pre_evaluation’ PHP filter

The gform_rule_pre_evaluation filter lets you modify a conditional logic rule before it gets evaluated. This filter has both a JavaScript version and a PHP version, both of which should be used.

Usage

To use the JavaScript version of the filter, use the following code:

gform.addFilter('gform_rule_pre_evaluation', function(rule, formId, conditionalLogic) {
    // your custom code here
    return rule;
});

For the PHP version of the filter, use the following code:

add_filter('gform_rule_pre_evaluation', function($rule, $form, $logic, $field_values, $entry) {
    // your custom code here
    return $rule;
}, 10, 5);

Parameters

  • rule (Javascript Object/PHP array): The current rule object.
  • formId (integer/string): The ID of the form in use.
  • conditionalLogic (Javascript Object): The current conditional logic object.
  • $form (Form Object): The form currently being processed. (PHP Version)
  • $logic (Conditional Logic Object): The conditional logic. (PHP Version)
  • $field_values (array): An array of dynamic population parameter keys with their corresponding values. (PHP Version)
  • $entry (Entry Object): The entry currently being processed, if available. (PHP Version)

More information

See Gravity Forms Docs: gform_rule_pre_evaluation
This filter was added in Gravity Forms v2.4.22. It’s available in both JavaScript and PHP versions, with the JavaScript version located in js/conditional_logic.js, and the PHP version in forms_model.php.

Examples

Example 1

This example shows how to replace field merge tags used in the conditional logic rule value.

gform.addFilter('gform_rule_pre_evaluation', function(rule, formId) {
    var mergeTags = GFMergeTag.parseMergeTags(rule.value);
    if (!mergeTags.length) {
        return rule;
    }
    rule.value = GFMergeTag.getMergeTagValue(formId, mergeTags[0][1], mergeTags[0][3]);
    return rule;
});

Example 2

For the PHP version, the code will be similar to the one below. This also demonstrates how to replace field merge tags used in the conditional logic rule value.

add_filter('gform_rule_pre_evaluation', 'filter_gform_rule_pre_evaluation', 10, 5);

function filter_gform_rule_pre_evaluation($rule, $form, $logic, $field_values, $entry) {
    if (!empty($rule['value']) && strpos($rule['value'], '{') !== false) {
        remove_filter('gform_rule_pre_evaluation', 'filter_gform_rule_pre_evaluation');
        if (empty($entry)) {
            $entry = GFFormsModel::get_current_lead();
        }
        $rule['value'] = GFCommon::replace_variables($rule['value'], $form, $entry, false, false, false, 'text');
        add_filter('gform_rule_pre_evaluation', 'filter_gform_rule_pre_evaluation', 10, 5);
    }
    return $rule;
}

Note: These snippets should be placed in a HTML field on your form or in a theme custom JavaScript file for the JavaScript version, and in the functions.php file of your active theme or a custom functions plugin for the PHP version.