Conditional logic if field value = another field value, show field [RESOLVED]

My goal is to give a warning when the state does not match another field or alternatively it does not match a page custom field.

I’ve tried a couple different ways but I’m confused about some things.

With this example,

add_filter( 'gform_pre_render', 'set_conditional_logic' );
add_filter( 'gform_pre_process', 'set_conditional_logic' );
function set_conditional_logic( $form ) {

    //Set conditional logic only for form 14
    if ( $form['id'] !== 14 ) {
        return $form;
    }
 
    foreach ( $form['fields'] as &$field ) {
        if ( $field->id == 2 ) {
            $field->conditionalLogic =
                array(
                    'actionType' => 'show',
                    'logicType' => 'all',
                    'rules' =>
                        array( array( 'fieldId' => 1, 'operator' => 'is', 'value' => 'First Choice' ) )
                );
        }
    }
    return $form;
}

I have trouble with the ‘value’ => ‘First Choice’))

I can’t seem to get it to find the value from another field. Alternatively I could compare the value against a custom field in the page meta but I can’t get it to find the page id that the form is currently on. I think because it may be outside the loop?

So I tried gform_is_value_match

My problem here is I don’t see where the comparison field id goes in this javascript example,

`gform.addFilter(` `'gform_is_value_match'` `, ` `function` `(isMatch, formId, rule) {`

`    ` `// do stuff`

 

`    ` `return` `isMatch;`

`} );`

And finally, in the PHP example, it includes a comparison of conditional logic views. I can’t quite figure out how to do just a comparison of values in this example.

`add_filter( ` `'gform_is_value_match'` `, ` `function` `( ` `$is_match` `, ` `$field_value` `, ` `$target_value` `, ` `$operation` `, ` `$source_field` `, ` `$rule` `) {`

`    ` `// do stuff`

 

`    ` `return` `$is_match` `;`

`}, 10, 6 );`

I may not need to do the PHP part. Any help is appreciated. If it’s too much I’ll just keep trying to figure it out. Thank you.

If you need to use a post custom field value, you could simply have a hidden field in the form that is dynamically populated using a custom field merge tag: https://docs.gravityforms.com/merge-tags/#custom-fields

You can also use a merge tag to get the value of the post/page ID the form is embedded on.

Then, you can use the value of the hidden field to do your comparison for conditional logic.

I have tried using a hidden field. But there is no way to do a conditional logic rule that solves my problem that way. And that is, if one field equals another field, show another field. So let’s say the hidden field says Louisiana. And they pick Pennsylvania, it shows a warning through an html field.

The merge tag doesn’t help me in the php code where I need to pull the post/page id from.

How do I use a hidden field to do the comparison logic. There is no option where one field equals another field that I can see?

I don’t really get the need for gform.addFilter approach here. Maybe you just need to think about this differently.

Actually, if you’re doing everything via php code, you don’t need a hidden field. In your pre_render function you can get the ID of the post using get_the_ID() and thru that, the value of any custom field:

$custom_field = get_post_meta(get_the_ID(),"custom-field", true);

Then use $custom field to populate a field and for the conditional logic check.

Quite possible that I do need to think of a different approach but I haven’t found one that works without going into php. The problem with the get_the_ID() function is that it is called outside the loop where it does not have the id yet. When I attempt to run the code later by adding an action, I get errors.

edit: also, using the global variable doesn’t work either.

It’s fine to use PHP for this.

Your pre_render function is able to use get_the_ID(). Why do you need to run a separate action function?

It seems to come out 0 for me. Maybe I’m doing something else wrong. I ran the action to run the code at a point where it does have the ID. I read it somewhere.

What does this action function look like and what is its purpose?

Here’s the example I followed

" Just put your code in some action, that is run later."

function my_wp_action_callback() {
    if ( is_home() ) {
        add_action( 'shoestrap_below_top_navbar', 'my_custom_below_top_navbar' );
    }
}
add_action( 'wp', 'my_wp_action_callback' );

So it runs the code later? It gave a bunch of errors that way. Oh I didn’t put the is_home() part btw.

I feel like that is a very twisted approach… what is the purpose of the action function? What is it supposed to do?

Run the code later when the ID is available. I don’t know, I’m just trying anything I can think of.

I am trying to understand what the code you’re trying to run later is supposed to do. What does that code that runs later look like?

Can it not be placed in your pre_render function?

I did it because it seemed to not be getting the ID inside the render function. It was coming up 0. So I was trying to find a way to run the code at a later time when the ID is available. I don’t think it was the right approach.

If it helps, here’s what my last attempt looks like,

add_filter( 'gform_pre_render', 'set_conditional_logic' );
add_filter( 'gform_pre_process', 'set_conditional_logic' );

function set_conditional_logic( $form ) {
    //Set conditional logic only for form 5
    if ( $form['id'] !== 5 ) {
        return $form;
    }
    if ( $field->id == 167 ) {
			 $custom_field = get_post_meta(get_the_ID(),"state", true);
            $field->conditionalLogic =
                array(
                    'actionType' => 'hide',
                    'logicType' => 'all',
                    'rules' =>
                        array( array( 'fieldId' => 167, 'operator' => 'is', 'value' => $custom_field ) )
                );
        }
    return $form;
  
}

Also, thanks for helping.

I could be wrong, but… the problem with this is it is PHP, and builds on loading unless there is something firing it. You need to use jQuery for this to do what you want, either pure jQuery or a jQuery that calls an AJAX function.

I use Scripts n Styles to put jQuery in my header/footer and it works great.

Oh ok. Thanks for the help, I’ll give that a shot.

That worked. Thanks. I’m a little new to both jQuery and PHP.

1 Like

Awesome! I am too, but jQuery does some cool stuff once you get the hang of it.