• Resolved vjdoon

    (@vjdoon)


    Hi, I am trying to create a form which will have a checkbox input. There will be 3 options in it (let’s say Option 1, Option 2 and Option 3), what I require is that Option 1 should always remain checked (user should not be able to uncheck it) and the user can either select Option 2/ Option 3 or both Option 2 and Option 3.

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Support Nebu John – WPMU DEV Support

    (@wpmudevsupport14)

    Hi @vjdoon,

    I hope you are keeping well today.

    An out-of-the-box solution is not available for this, however, I have asked our code experts to check if a workaround could be suggested. We’ll inform you here if a workaround could be suggested.

    Kind Regards,
    Nebu John

    Plugin Support Dmytro – WPMU DEV Support

    (@wpmudevsupport16)

    Hello @vjdoon,

    Please try adding the following PHP snippet.

    Save it as a PHP file, and upload it to /wp-content/mu-plugins/ directory, to make it run as a must use plugin:

    <?php
    add_action( 'wp_footer', 'wpmudev_keep_checkbox_disabled', 9999 );
    function wpmudev_keep_checkbox_disabled() {
        global $post;
        if ( is_a( $post, 'WP_Post' ) && ! has_shortcode( $post->post_content, 'forminator_form' ) ) {
            return;
        }
    	?>
    	<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.
                    $('#checkbox-1 input:checked').attr('disabled', 'disabled');
                }
            });
        });
        </script>
        <?php
    }

    Replace 6 with your form ID on this line:

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

    and checkbox-1 with your checkbox field name in this part:

    $('#checkbox-1 input:checked').attr('disabled', 'disabled');

    Let us know if you have any questions!
    Best Regards,
    Dmytro

    Thread Starter vjdoon

    (@vjdoon)

    Hi,
    Thanks for the code snippet, I tried it on this link https://mytestsite.in/rscdemo/temp/ but it is not working. ( In this form, my requirement is that “General Entry” option of Entry type always remains checked and the user should not be able to uncheck it

    Plugin Support Jair – WPMU DEV Support

    (@wpmudevsupport15)

    Hi @vjdoon,

    I hope you are doing well today!

    Please share an export of the form with us, so that we can check further.

    You can find more info on how to export the form here : https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#import-export

    After exporting, please share the code using a services such as https://pastebin.com or https://justpaste.it/ which is free to use.

    Please always make sure to use such service to share the code and don’t post the code here directly as it will most likely be unusable.

    Kind regards,
    Zafer

    Thread Starter vjdoon

    (@vjdoon)

    Hi,

    please find the exported form on this link https://jpst.it/3whm5

    Plugin Support Jair – WPMU DEV Support

    (@wpmudevsupport15)

    Hi again @vjdoon,

    We have tested the code snippet after importing your form and it was working well. Your form ID seems to be 658, so the code should look like this.

    <?php
    add_action( 'wp_footer', 'wpmudev_keep_checkbox_disabled', 9999 );
    function wpmudev_keep_checkbox_disabled() {
        global $post;
        if ( is_a( $post, 'WP_Post' ) && ! has_shortcode( $post->post_content, 'forminator_form' ) ) {
            return;
        }
    	?>
    	<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-658' ) { // Please change the form ID.
                    $('#checkbox-1 input:checked').attr('disabled', 'disabled');
                }
            });
        });
        </script>
        <?php
    }

    You can find more information below on how to use mu-plugins.
    https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins
    and
    https://www.ads-software.com/support/article/must-use-plugins/

    Kind regards,
    Zafer

    Thread Starter vjdoon

    (@vjdoon)

    The code is working as intended,but upon submitting the form without checking any other box it gives an error “Error: Your form is not valid, please fix the errors!”

    Plugin Support Jair – WPMU DEV Support

    (@wpmudevsupport15)

    Hi again @vjdoon,

    We are checking the issue with our SLS (Second Line Support) team and will inform you accordingly.

    Thanks for the patience while we are looking into this.

    Kind regards,
    Zafer

    Plugin Support Jair – WPMU DEV Support

    (@wpmudevsupport15)

    Hi again @vjdoon,

    Please replace the following line in code

    $('#checkbox-1 input:checked').attr('disabled', 'disabled');

    with this and try again

    $('#checkbox-1 input:checked').on('click', function(){
    $(this).prop("checked", true);
    });

    Kind regards,
    Zafer

    Thread Starter vjdoon

    (@vjdoon)

    works perfectly! thanks

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘one checkbox always ticked’ is closed to new replies.