• Resolved sinned1

    (@sinned1)


    Hi there! I’m trying to do this on my checkbox fields for my Form: https://imgur.com/MXNYuhX
    The thing is I have read your API documentation and I noticed that an update_form_field() method exists and I was wondering if this function could be used when an option is added and the button is clicked to do so. I already made this JS function copying every attribute the field needs with the proper information to be a different option and do not mix any behaviour, but it just for the Front because if I submit the Form and one of the new options is selected the submit action returns an error and it’s not completed until I deselect this options.
    If there is a way to do this I’m glad to read it and try it, thanks in advance!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @sinned1

    I hope you are doing well.

    You can use something like this:

    <?php
    
    function handle_my_field_option_update()
    {
    
        $value = 'test';
        $label = 'test';
    
        $new_options = array(
            'label' => $label,
            'value' =>  $value,
            'limit' => '',
            'key'   => forminator_unique_key(),
        );
    
        $form_id = 26197;
        $elem_id = 'checkbox-1';
        $to_array = true;
        $current_field = Forminator_API::get_form_field($form_id, $elem_id, $to_array);
    
        $current_field['options'][] = $new_options;
    
        Forminator_API::update_form_field($form_id, $elem_id, $current_field);
    }
    
    // register ajax
    
    add_action('wp_ajax_handle_my_field_option_update', 'handle_my_field_option_update');
    add_action('wp_ajax_nopriv_handle_my_field_option_update', 'handle_my_field_option_update');
    
    add_filter('wp_footer', function () {
    
        
        ?>
    
        <script>
            jQuery(document).ready(function($) {
                $.ajax({
                    url: '<?php echo admin_url('admin-ajax.php'); ?>',
                    type: 'POST',
                    data: {
                        action: 'handle_my_field_option_update',
                    },
                    success: function(response) {
                        console.log(response);
                    }
                });
            });
        </script>
       
    
    <?php
    
    }, 9999);

    This is a simple test that updates the option by adding “test” for label and value.

    But some consideration here.

    – This means updating the Forminator database, for example you have two people submitting the form at the same time you run into issues.
    – This will print the custom entry for the next user unless you reset the default values after submission, but let’s say your user leaves the page then it will cause the same problem. A possible solution for it is to reset the options on form load and for that, you can use this hook https://gist.github.com/wpmudev-sls/6f1c8989a170b8f02026354ffe5ad24f#file-forminator-render-select-from-csv-php-L18

    A better option is to detach the “other” option from your field, you can use the repeater https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#field-group-repeater and create the “Add your option” as a group which allow you to create the values on the fly and will be handled by Forminator by default.

    Best Regards
    Patrick Freitas

    Plugin Support Dmytro – WPMU DEV Support

    (@wpmudevsupport16)

    Hello @sinned1,

    I hope you’re doing good today!

    We’ll be marking the issue as resolved, as we didn’t hear back from you for a while. In case you need our further assistance, please let us know by replying in this thread.

    Best Regards,
    Dmytro

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.