Support Forum
Support › MB Frontend Submission › Conditional logic
Hi - sorry for the flurry of questions - been working on a form.
I have created 'pages' on a cloneable group field so easier to toggle through. Pages work.
However, I have conditional logic that determines fields in page 4 from the field in page 1. So it is a dropdown that populates the needed fields later on. Eg
This value
[
'name' => __( 'Publication format', 'your-text-domain' ),
'id' => $prefix . 'academicoutput',
'type' => 'taxonomy_advanced',
'taxonomy' => ['output'],
'field_type' => 'select_advanced',
'placeholder' => __( 'Select publication type...', 'your-text-domain' ),
'required' => true,
'add_new' => true,
'class' => 'ref-p10-1 ref-label ref-group',
'columns' => 6,
],
Determines the value of a lot of fields like this:
[
'name' => __( 'Book name', 'your-text-domain' ),
'id' => $prefix . 'academicbook_name',
'type' => 'text',
'columns' => 6,
'visible' => [ 'academicoutput', 'in', [ 9207, 9208 ] ],
'class' => 'ref-p10-4 ref-label ref-group',
],
However, when that value was selected all the fields showed up beneath my page one and not on their page despite CSS rules hiding their class.
So as a workaround, I added a hidden field that dynamically populates with the current page so that I can add an extra condition to my conditional logic, only if that value = their page will it show. I can confirm that this field populates correctly.
So then I added an extra condition into my conditional logic so that it only shows when the value of the hidden field is equal to their page.
[
'name' => __( 'Journal name', 'your-text-domain' ),
'id' => $prefix . 'academicjournal',
'type' => 'taxonomy_advanced',
'taxonomy' => ['journal'],
'field_type' => 'select_advanced',
'add_new' => true,
'columns' => 12,
'visible' => array(
'when' => array(
array('academicoutput','in', array(9212, 13304)),
array('academicreference_group_current_selection', 'ref-p10-4') //This is a group field and academicreference_group is the group ID, current_selection is the field.
),
'relation' => 'and'
),
'class' => 'ref-p10-4 ref-label ref-group',
],
But this doesn,t work. Same issue. When the value is selected, the fields show up directly below the first page fields. And I am really stumped why its doing that considering there is a condition that it is not meeting.
Hello Yasmine,
Can you please share some screenshots or a screen record of the issue? Do you use a multi-step form, show the current step (page) and hide other ones?
Sure thing: https://jam.dev/c/babc47aa-8cc7-4bec-9995-3830a6fda469
I do have a multistep, but it should be hiding the classes unless on that class. I also added the condition that the field should only show when
'visible' => array(
'when' => array(
array(
array('academicoutput', 9212),
array('academicoutput', 13304),
'relation' => 'or'
),
array('current_selection', 'ref_p10-4')
),
'relation' => 'and'
),
But it was showing (as you can see in video) when current_selection','=', 'ref_p10-1'
Hello,
MB Conditional Logic does not support nested relations as in your code, please remove one relation and recheck this issue. Read more in the documentation https://docs.metabox.io/extensions/meta-box-conditional-logic/#multiple-conditions
Yes, the issue is still there when I remove the array('current_selection', =, 'ref_p10-4').
The only reason I added it as an additional condition, was to try and get rid of this issue.
I also don't understand what you mean, the documentation you shared says that you can add multiple conditions? And I believe I followed the documentation?
Hi, I went into the builder and used the builder to create my conditional logic so I could ensure I had the right format. I then copied this format across eg
'visible' => [
'when' => [['academicoutput', 'in', ['9212', '13304']], ['current_selection', '=', 'ref_p10-4']],
'relation' => 'and',]
],
But I still have two issues:
1. Populating a field automatically with js does not trigger the conditional logic. You need to go in and retype a letter and then it happens.
2. Why does it not work correctly when there are two conditions?? You shared with me documentation about how to have multiple conditions, and I have made sure I used the exact format, so I really don't get it.
But I just want an answer to how I can get this working. What exactly do I need to do?
And just to take a step back at the bigger issue, as this is what Im really trying to solve:
I have made some navigatable sections on a cloneable group field. Currently (and when I only have one conditional logic condition), when the value is selected, it shows beneath the first page - despite having CSS there hiding its class. So I added an additional condition, which I want just to keep it hidden until you reach the page. This is governed by a hidden field that is populated by js. The value is correct. However, this lead to the issue of conditional logic not being triggered when a field is populated automatically. And it also did not fix my problem of keeping the field hidden until the right moment in the form.
Hello,
Is the field current_selection
or academicoutput
shown/hidden by another logic or custom JS code? If yes, the nested conditional logic does not work properly. You can refer to this topic https://support.metabox.io/topic/conditional-logic-advanced-taxonomy-with-2-rules/?swcfpc=1
They are hidden by the tab pages eg:
$('.form-nav').on('click', function(e) {
e.preventDefault();
$('.rwmb-tab-panel').hide();
var targetPanel = $(this).data('target');
$('.rwmb-tab-panel-' + targetPanel).show();
});
$('body').on('click', '.ref-nav', function(e) {
e.preventDefault();
let currentGroup = $(this).closest('.rwmb-group-clone');
let sectionToHide = '.' + $(this).attr('data-current');
let sectionToShow = '.' + $(this).attr('data-target-group');
let sectionToPopulate = $(this).attr('data-target-group');
currentGroup.find(sectionToHide).hide();
currentGroup.find(sectionToShow).show();
});`
But not by another field as per the issue on the other topic. The custom_selection
is a hidden field thats only purpose is to update so I can use that value. And academicoutput
has no other conditional logic to determine its visibility beyond the page logic. Is that what you mean by nested? Or do you mean because it is a group field?
I also tried using a custom callback to set my own conditional logic and that wouldn't work either...
Hello,
Is that what you mean by nested?
Yes, please check again when both fields current_selection
or academicoutput
are on the same page and not hidden by a custom JavaScript code (step in your case).
FYI, the conditional logic of Meta Box also uses JavaScript to show/hide the field.
This issue has taken me too much time, so I am finding a workaround and giving up.
But I able to work in the original Conditional Logic code - as if I can then I can just modify my needs there so no conflict. If so, how can I do that
I'm sorry about that but I cannot check if the MB conditional logic works with all custom code you made on your site, it is beyond our scope of support.
To reproduce the issue, please just create some simple fields and test it on the same page without the custom code as I mentioned above https://support.metabox.io/topic/conditional-logic/?swcfpc=1#post-43678