Custom Fields
Re-Envisioned
Support Meta Box Conditional Logic Visible/hidden with id outside group

This topic contains 4 replies, has 2 voices, and was last updated by  Anh Tran 1 week, 4 days ago.

  • Creator
    Topic
  • #13617

    Charlin
    Participant

    Hello,

    I try to have a conditionnal logic with fields in group with an option outside the group.

    $meta_boxes[] = array(
          'title'  => __( 'Test', 'extensa' ),
          'post_types' => array( 'extensa-venuerent' ),
          'fields' => array(
            array(
              'name'       => 'Number',
              'id'    => "nbr",
              'type'       => 'select',
              'options' => array(
                '1' => '1',
                '2' => '2',
                '3' => '3',
                '4' => '4',
              ),
            ),
            array(
              'name'       => 'Title room 1',
              'id'    => "title_room_1",
              'type'       => 'text',
            ),
            array(
              'visible' => array("nbr", '>=', '2'),
              'name'       => 'Title room 2',
              'id'    => "title_room_2",
              'type'       => 'text',
            ),
            array(
              'visible' => array("nbr", '>=', '3'),
              'name'       => 'Title room 3',
              'id'    => "title_room_3",
              'type'       => 'text',
            ),
            array(
              'visible' => array("nbr", '>=', '4'),
              'name'       => 'Title room 4',
              'id'    => "title_room_4",
              'type'       => 'text',
            ),
            array(
              'name'  => 'Items',
              'id'     => 'item',
              'type'   => 'group',
              'clone'  => true,
              'sort_clone' => true,
    
              'fields' => array(
                array(
                  'name'       => 'Value room 1',
                  'id'    => "value_room_1",
                  'type'       => 'text',
                ),
                array(
                  'visible' => array("nbr", '>=', '2'),
                  'name'       => 'Value room 2',
                  'id'    => "value_room_2",
                  'type'       => 'text',
                ),
                array(
                  'visible' => array("nbr", '>=', '3'),
                  'name'       => 'Value room 3',
                  'id'    => "value_room_3",
                  'type'       => 'text',
                ),
                array(
                  'visible' => array("nbr", '>=', '4'),
                  'name'       => 'Value room 4',
                  'id'    => "value_room_4",
                  'type'       => 'text',
                ),
              ),
            ),
          ),
        );

    The fields outside the group with a “visible” option working great, if I choose nbr = 2, I only have title_room_1 and title_room_2 visible but in group, value_room_3 and value_room_4 are always visible.

    I don’t know if my explanations are clear 🙁

    Any clue ? It’s possible ?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Author
    Replies
  • #13618

    Charlin
    Participant

    I tried to make a custom callback but it’s seems doesn’t work too.

    'visible' => array("my_custom_callback()", true),

    and JS

    console.log('my custom callback init');
    function my_custom_callback(){
        console.log('my custom callback test!');
        return false;
    }

    In console I can see “My custom callback init” but never “my custom callback test!” and the element with the visible option is always visible.

    Something broked with conditionnal logic ? :s

    I found a fix in “conditional-logic.js” in the plugin folder.

    Line 234 to 236

    if ( ! dependentFieldSelector ) {
      // return;
    }

    If I comment line 235, my condition work.

    #13636

    Anh Tran
    Keymaster

    Hi Charlin,

    The problem is when the group is cloneable, the conditions is “scoped” inside the clone only. Think about a situation when you have a group of 2 fields A and B. B is visible when A has a value “a”.

    What about if we clone this group and have multiple pairs (A,B). A1 has a value “a”, then all B will be visible without scoping, which is weird.

    I’ll try to think about your situation and provide an update for this specific case.

    #13639

    Charlin
    Participant

    Hi Anh,

    I understand if it can’t work I can accept it 😉

    For the custom callback, it’s a bug or not ? I miss something ?

    #13653

    Anh Tran
    Keymaster

    To me, it’s more likely a bug. Since it still checks for the selector of dependent element. I’ll check and fix that.

Viewing 4 replies - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.