• Resolved eimintas

    (@eimintas)


    Hi,
    Is it possible to display certain choices in a second dropdown field based on what is selected in the first dropdown field?
    I read the documentation and couldn’t find anything (using forminator free version)

    I’ve also tried using css and javascript, but I could only get the selected value from both fields simultaneusly (not separate), using custom css classes does not return a value, only using auto generated internal css class or ID works for the fields.

    Does anyone have a solution? I want to hide certain values on the second dropdown field based on what is selected on the first dropdown field.

    The page I need help with: [log in to see the link]

Viewing 9 replies - 1 through 9 (of 9 total)
  • Hi @eimintas ,

    Greetings! Yes, the Forminator plugin offers a generous feature known as ‘Conditional Display,’ which is also referred to as ‘Visibility’ or ‘Conditional Logic.’ You can find detailed information on how to use this feature in our documentation [https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#Forminator-Conditional-Logic].

    Feel free to explore and make the most of this powerful functionality!

    Best regards,
    Olumide Akintunde

    Thread Starter eimintas

    (@eimintas)

    Thanks for your reply,

    Althought I would like to hide certain Choices (not the whole field), for example: first choice field is Country and the second one would be City, so I would want to show certain cities based on chosen country

    Plugin Support Laura – WPMU DEV Support

    (@wpmudev-support8)

    Hi @eimintas

    I hope you’re well today!

    Currently this kind of behavior is not supported by our plugin out of the box. The conditional logic can only be applied to entire fields and not specific parts of it.

    However, there’s a custom code that should help and here’s how to use it:

    1. First, you need to add following code as MU plugin:

    <?php
    add_action('wp_footer', 'wpmudev_disable_select_options', 9999);
    function wpmudev_disable_select_options() {
        if ( ! is_page( 'aanmeldformulier2' ) ) {
            return;
        }
    	?>
    	<style>
    	.select2-results__option--disabled {
    		display: none !important;
    	}
    	</style>
    	<script type="text/javascript">
    	jQuery(document).ready(function($) {
    		setTimeout(function() {
    			$('.forminator-custom-form').trigger('after.load.forminator');
    		}, 100);
    		
    		$(document).on('after.load.forminator', function(event, form_id) {
    			if ( event.target.id == 'forminator-module-623' ) { //Please change the form ID
    				$('.wpmudev-relative-select-field select').each(function() {
    					$(this).on('change', function(){
    						$(this).find('option:selected').each(function() {
    							var value = this.value;
                                if (value !== "") {
                                    var id = $(this).parent().parent().parent().prop('id');
                                    var options = $('.wpmudev-relative-select-field:not(#' + id + ') option[value=' + value + ']');
                                    options.prop('disabled', true);
                                }
    						});
    					});
    				});
    			}
    		});
    	});
    	</script>
    	<?php
    }

    – create an empty file with a .php extension (e.g. “forminator-relative-select.php”)
    – copy and paste code into it
    – save the code and upload it to the “/wp-content/mu-plugins” folder of your site’s WordPress install

    Note: if there’s no “mu-plugins” folder directly in “wp-content”, just create an empty one first.

    2. Then you need to “configure” the code. That’s actually pretty simple:

    in this line of code

    if ( event.target.id == 'forminator-module-623' ) { //Please change the form ID

    you need to replace number 623 with your form ID; form ID is the number you see in form’s shortcode.

    3. and finally, you need to configure the form by adding this (note no dot in front!)

    wpmudev-relative-select-field

    into “Additional CSS classes” setting in “Styling” configuration of each of your “select” fields on that form.

    Once you update the form and clear all caches, it should do the trick. Note: if it doesn’t work out of the box, you may need to switch on or off (opposite to as it is) the “Load Form using Ajax” switch in the “Behavior” settings of the form.

    Best regards,
    Adam

    Thread Starter eimintas

    (@eimintas)

    Hey, @wpmudev-support8

    I appreciate you replying to my request.

    I’ve done everything based on your instructions. Currently, i see no difference in how the form works. How do i configure which <options> should be hidden based on choice? Because I assume this code only returns the selected value from chosen choice fields? Or am I missing something.
    Previously i tried code such as : $(‘.forminator-select2 option[value=”myValue”]’).hide(); ,but none could hide certain <options>. Thanks

    • This reply was modified 2 years, 8 months ago by eimintas.
    • This reply was modified 2 years, 8 months ago by eimintas.
    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @eimintas,

    How do i configure which <options> should be hidden based on choice?

    Please do note that what was shared in the snippet is more of a workaround, the above doesn’t show/hide the options based on the specific choices.

    The given snippet will only remove the options from other select fields if it is selected in any of the existing select fields.

    Could you please check based on the mentioned workflow and see whether it fits your needs? If yes, we could check further on what’s missed on the given snippet on your side as it should have worked as instructed.

    Setting up a form to hide/show options based on a selection of different Select fields will be complex as it depends upon the options and their combinations. I’m afraid trying to provide a snippet which works as above will require a lot of custom coding and outside our support scope.

    Best Regards,

    Nithin

    Thread Starter eimintas

    (@eimintas)

    The given snippet will only remove the options from other select fields if it is selected in any of the existing select fields.

    Could you please check based on the mentioned workflow and see whether it fits your needs? If yes, we could check further on what’s missed on the given snippet on your side as it should have worked as instructed.

    Hi @wpmudevsupport11

    I suppose the function of the given snippet will do for now, but I’ve tried it with 2 choice fields, the second one has the same choices (and internal values) + additional values, but upon selection nothing is being hidden in either of the fields.
    I’ve double checked everything, the form name is correct inside the PHP code, CSS classes are applied. Cleared cache and swapped form load method. Browser console is not returning any errors/messages

    Plugin Support Williams – WPMU DEV Support

    (@wpmudevsupport3)

    Hi @eimintas,

    Hope this message finds you well.

    I found an alternative mu-plugin and see if that you can customize it according to your needs.

    The mu-plugin below is for 2 selects that contain the same values in both, if value 1 is selected in {select-1} then the value 1 will be removed in {select-2}, maybe this is closer to what you are trying to approach.

    add_action('wp_footer', 'wpmudev_select_option_display_manage', 9999);
    function wpmudev_select_option_display_manage() {
    	global $post;
    	if ( is_a( $post, 'WP_Post' ) && !has_shortcode($post->post_content, 'forminator_form') ) {
    		return;
    	}
    	?>
    	<style>
    	.select2-results__option--disabled {
    		display: none !important;
    	}
    	</style>
    	<script type="text/javascript">
    	jQuery(document).ready(function($){
    		setTimeout(function() {
    			$('.forminator-custom-form').trigger('after.load.forminator');
    		}, 100);
    		
    		$(document).on('after.load.forminator', function(event, form_id) {
    			if ( event.target.id == 'forminator-module-6' ) { //Please change the form ID
    				//Mapping of fields
    				var select_fields = {};
    				select_fields['#select-1'] 	= ['#select-2'];
    				
    				jQuery.each(select_fields, function(index, item) {
    					//On selection
    					$(index + ' select').on('select2:select', function(e) {
    						var sel_value = $(index + ' select').find(":selected").val();
    						$(item).each(function(i, ind) {
    							$(ind + ' select option').each(function() {
    								if ( $(this).val() == sel_value ) {
    									$(this).attr('disabled', 'disabled');
    								} else {
    									$(this).removeAttr('disabled');
    								}
    							});
    						});
    					});
    				});
    			}
    		});
    	});
    	</script>
    	<?php
    }

    Please take a look and let us know.

    Best regards,
    Laura

    Thread Starter eimintas

    (@eimintas)

    Hi @wpmudevsupport3

    Very grateful for your provided code
    I’ve managed to get it working by adding it inside HTML element using Elementor, because the function inside mu-plugins was not working for some reason :D.
    Thank you again, I will try to customise it to achieve my original request.

    Thread Starter eimintas

    (@eimintas)

    <style>
        .select2-results__option--disabled {
            display: none !important;
        }
    </style>
    
    <script type="text/javascript">
        jQuery(document).ready(function ($) {
            setTimeout(function () {
                $('.forminator-custom-form').trigger('after.load.forminator');
            }, 100);
    
            $(document).on('after.load.forminator', function (event, form_id) {
                if (event.target.id == 'forminator-module-1431') { // Change the form ID
                    // Mapping of fields
                    console.log("loaded");
                    var select_fields = {};
                    select_fields['.wpmudev-relative-select-field-1'] = ['.wpmudev-relative-select-field-2'];
    
                    // Define the mapping of values from the first dropdown to the corresponding values in the second dropdown
                    var valueMapping = {
                        'Aberdeen-(GB)': ['Kirkwall-(GB)', 'Lerwick-(GB)'],
                        'Ajaccio-(FR)': ['Marseille-(FR)'],
                        'Ancona-(IT)': ['Igoumenitsa-(GR)', 'Patras-(GR)', 'Split-(HR)'],
                        // Add more mappings as needed
                    };
    
                    jQuery.each(select_fields, function (index, item) {
                        // On selection
                        $(index + ' select').on('change', function () {
                            var sel_value = $(this).val(); // Get the selected value from the first dropdown
                            var mappedValues = valueMapping[sel_value];
    
                            $(item + ' select option').each(function () {
                                var option_value = $(this).val();
                                // Check if the option value is in the mapped values array
                                if (mappedValues.indexOf(option_value) !== -1) {
                                    $(this).removeAttr('disabled');
                                } else {
                                    $(this).attr('disabled', 'disabled');
                                }
                            });
                        });
                    });
                }
            });
        });
    </script>

    Great news, with the help of ChatGPT and the previous code provided, i’ve managed to achieve my goal, for those who are trying to make a similiar function.

    select_fields are css classes for dropdown 1 and dropdown 2

    var valueMapping are the choices to be shown in dropdown 2 field, based on choice selected in the first one, the remaining choices are hidden (use internal values for choices).

    forminator-module-1431 replace 1431 with your own form ID (from form shortcode)

    Thanks again for helping me πŸ˜€

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

The topic ‘Conditional display for dropdown choices’ is closed to new replies.