Custom Fields
Re-Envisioned
Support Meta Box Conditional Logic Conditional Logic doesn't work inside group field

  • Creator
    Topic
  • #30676
    Resolved Jugibur
    Participant

    Hello,

    I wish to have a conditional logic inside a group field but it doesn't work.

    So I take the basic example in your docs and put it inside a group field.
    The logic does not seem to work here either. Or do I something wrong?

    
    add_filter( 'rwmb_meta_boxes', function( $meta_boxes ) {
        $meta_boxes[] = array(
            
            'title' => 'Brands and Products',
            'post_types' => ['page'],
            'context' => 'side',
            
            'fields' => array(
                array(
                    'id' => 'test_group',
                    'type' => 'group',
                    'fields' => array(
                        array(
                            'id'    => 'brand',
                            'name'  => 'Brand',
                            'desc'  => 'Pick Your Favourite Brand',
                            'type'  => 'select',
                            'options' => array(
                                'Apple'         => 'Apple',
                                'Google'        => 'Google',
                                'Microsoft'     => 'Microsoft'
                            )
                        ),
                        array(
                            'id'    => 'apple_products',
                            'name'  => 'Which Apple product that you love?',
                            'type'  => 'radio',
                            'options' => array(
                                'iPhone'    => 'iPhone',
                                'iPad'      => 'iPad',
                                'Macbook'   => 'Macbook',
                                'iWatch'    => 'iWatch'
                            ),
    
                            // Hide this field when user selected a brand that's not 'Apple'
                            'hidden' => array( 'brand', '!=', 'Apple' )
                        )
                    )
                )
            )
        );
    
        return $meta_boxes;
    } );
    
    

    Thanks for your help
    Jürgen

Viewing 4 replies - 1 through 4 (of 4 total)
  • Author
    Replies
  • #30677
    Long Nguyen
    Moderator

    Hi,

    The subfield has the ID groupID_subfieldID, you should indicate it in the condition like this

    'hidden' => array( 'test_group_brand', '!=', 'Apple' )

    Refer to this topic https://metabox.io/support/topic/conditional-logic-does-not-work/

    #30680
    Jugibur
    Participant

    Oh that is great, thanks for your help!

    PS: In the documentation it says we have to use 'contains' for some types, i.e. select multiple instead of '=='.
    In my case I have just a single value select field (like in the example) but it likewise need 'contains' for value checking.
    I'm wondering that the negation '!=' works fine here.

    #30690
    Long Nguyen
    Moderator

    Hi,

    The normal operators = or != also work with your case. Use the operator contains when you set the setting 'multiple' => true, to select multiple options.

    array(
        'id'    => 'brand',
        'name'  => 'Brand',
        'desc'  => 'Pick Your Favourite Brand',
        'type'  => 'select',
        'options' => array(
            'Apple'         => 'Apple',
            'Google'        => 'Google',
            'Microsoft'     => 'Microsoft'
        ),
        'multiple'  => true,
    ),
    #30695
    Jugibur
    Participant

    Oh sorry, I've overloocked that it's not '==' but '=' to do a simple compare.
    It works now – thanks for your hint!

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.