Conditional Logic doesn't work inside group field
Support › MB Conditional Logic › Conditional Logic doesn't work inside group fieldResolved
- This topic has 4 replies, 2 voices, and was last updated 3 years, 7 months ago by
Jugibur.
-
AuthorPosts
-
September 9, 2021 at 2:56 PM #30676
Jugibur
ParticipantHello,
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ürgenSeptember 9, 2021 at 3:51 PM #30677Long Nguyen
ModeratorHi,
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://support.metabox.io/topic/conditional-logic-does-not-work/
September 9, 2021 at 6:24 PM #30680Jugibur
ParticipantOh 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.September 10, 2021 at 12:39 PM #30690Long Nguyen
ModeratorHi,
The normal operators
=
or!=
also work with your case. Use the operatorcontains
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, ),
September 10, 2021 at 2:50 PM #30695Jugibur
ParticipantOh sorry, I've overloocked that it's not '==' but '=' to do a simple compare.
It works now – thanks for your hint! -
AuthorPosts
- You must be logged in to reply to this topic.