• Resolved abozzi

    (@abozzi)


    Hello,

    I am using a multi-step form on my site and need to implement custom validation for the field text-1, which is located on the first step of the form.

    I attempted to use the forminator_custom_form_submit_errors filter, but it only triggers upon submitting the final step, not when submitting the first step.

    I have reviewed the documentation and even inspected the code manually but couldn’t find a suitable filter or action that triggers validation during step-by-step progression.

    Additionally, when the error is triggered using forminator_custom_form_submit_errors, it displays correctly, but the entire form is cleared, as if all previously entered data is lost.

    Is there a way to achieve custom validation during the submission of individual steps? And how can I prevent the form data from being wiped out upon triggering a validation error?

    Thank you in advance for your assistance,
    Alessandro

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

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter abozzi

    (@abozzi)

    add_filter('forminator_custom_form_submit_errors', function( $submit_errors, $form_id, $field_data_array ) {

    if ($form_id = 15837) {

    global $wpdb;

    $form_cf = $field_data_array[3]['value'];

    $days_limit = (int) get_option('forminator_days_limit', 45);

    $date_limit = date('Y-m-d H:i:s', strtotime("-{$days_limit} days"));

    $sql = $wpdb->prepare(

    "SELECT COUNT(*)

    FROM {$wpdb->prefix}frmt_form_entry_meta AS fem

    WHERE fem.meta_key = 'text-1'

    AND fem.meta_value = %s

    AND fem.date_created >= %s",

    $form_cf,

    $date_limit

    );

    $results = $wpdb->get_results($sql);

    if (!empty($results) && $results[0]->{'COUNT(*)'} > 0) {

    $submit_errors[]['text-1'] = __( 'Error Message');

    return $submit_errors;

    }

    }

    }, 10, 3 );

    That’s my code right now.

    Plugin Support Laura – WPMU DEV Support

    (@wpmudev-support8)

    Hi @abozzi ,

    I hope you’re doing well.

    We have this hook apply_filters( 'forminator_pagination_step_markup', $html, $step, $label, $element_id )

    That hook is used to check each step.

    how can I prevent the form data from being wiped out upon triggering a validation error?

    Could you please confirm if you have the inline validation enabled and if it helps? https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#validation

    In case you need further help, could you please give us an example of how you would be using the validation on text-1? We’ll try to check further.

    Best Regards,
    Williams Valerio

    Thread Starter abozzi

    (@abozzi)

    Hi Valerio,

    thanks for your help.

    I confirm you that i have the inline validation enabled.

    The purpose of the custom validation is to check on step 1 submission if the ‘text-1’ field, which is the Personal ID Code, has not already been submitted within the last 45 days. If submissions are found, I would like to prevent the user from submitting the form.

    Do you think that forminator_pagination_step_markup is the right choice?

    Best Regards,
    Alessandro

    Thread Starter abozzi

    (@abozzi)

    Hi @wpmudev-support8 ,

    i tried using forminator_pagination_step_markup but i don’t see how to use it to validate the text-1 input field.

    Could you please confirm if you have the inline validation enabled and if it helps? https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#validation

    Do you suggest to disable the inline validation?

    Thanks,
    Alessandro

    Plugin Support Saurabh – WPMU DEV Support

    (@wpmudev-support7)

    Hello @abozzi

    Hope you’re doing well today!

    i tried using forminator_pagination_step_markup but i don’t see how to use it to validate the text-1 input field.

    Could you please share the snippet with how you’re using the hook? Kindly note that further script customisation would be out of our scope of support that we can provide; However, we can still try to help with hooks/filters and share suggestions in case there is anything missing on the custom snippet you’re using.

    Awaiting your response.

    Kind Regards,
    Saurabh

    Thread Starter abozzi

    (@abozzi)

    Hello @wpmudev-support7 ,

    Thank you for your response!

    I’ve tried using the forminator_pagination_step_markup hook to validate the text-1 input field, but I can’t seem to access the user-submitted data to perform the validation. Here’s the snippet I’m working with:

    add_filter( 'forminator_pagination_step_markup', 'custom_forminator_step_validation', 10, 4 );

    function custom_forminator_step_validation( $html, $step, $label, $element_id ) {
    if ( $step === 1 ) {
    // Attempting to retrieve the 'text-1' field value
    if ( isset( $_POST['text-1'] ) && ! empty( $_POST['text-1'] ) ) {
    $text_value = sanitize_text_field( $_POST['text-1'] );


    // Example validation: Checking if the input contains at least 5 characters
    if ( strlen( $text_value ) < 5 ) {
    $html .= '<div class="forminator-error">The text-1 field must contain at least 5 characters to proceed.</div>';
    }
    } else {
    $html .= '<div class="forminator-error">The text-1 field is required to proceed.</div>';
    }
    }

    return $html;
    // Example validation: Checking if the input contains at least 5 characters
    if ( strlen( $text_value ) < 5 ) {
    $html .= '<div class="forminator-error">The text-1 field must contain at least 5 characters to proceed.</div>';
    }
    } else {
    $html .= '<div class="forminator-error">The text-1 field is required to proceed.</div>';
    }
    }

    return $html;
    }

    However, this doesn’t seem to work as expected because I believe the hook is applied to the markup and not to the form submission process itself. The problem is I can’t access the submitted data (text-1) from this hook.

    Could you please confirm if this hook is suitable for my use case, or if there’s another hook/filter I should be using to validate the field before allowing progression to step 2?

    Thanks again for your support!

    Best regards,
    Alessandro

    Plugin Support Nebu John – WPMU DEV Support

    (@wpmudevsupport14)

    Hi @abozzi,

    This type of validation requires JavaScript for inline validation or can be performed only at the end of the submission process. The current approach in your code will not work because the text-1 field has not been submitted yet, meaning it will not be available in the $_POST variable.

    Providing an exact code for this would be complex and falls outside the scope of our support. To achieve this, you may need to hire a developer to create the required custom code. If needed, you can explore the WordPress jobs directory here: https://jobs.wordpress.net/

    If you have any further questions about this, feel free to reach out to us at [email protected] with the following subject line.

    Subject: ATTN: WPMU DEV support - wp.org

    Best Regards,
    Nebu John

    Thread Starter abozzi

    (@abozzi)

    Hello,

    writing this for anyone in the future seeing this thread.

    Support didn’t help unfortunately but i found out reading the JS file /wp-content/plugins/forminator/assets/js/front/front.pagination.js how to avoid the normal behavior of the event forminator.front.pagination.move and add a custom validation of a selected field.

     

    add_action(

    'wp_footer',

    function() {

    if ( ! wp_script_is( 'forminator-front-scripts' ) ) {

    return;

    }

    ?>

    <script type="text/javascript">

    (function ($) {

    $(document).ready(function () {

    setTimeout(function () {

    $('.forminator-custom-form').trigger('after.load.forminator');

    }, 100);

    $(document).on('after.load.forminator', function (e, form_id) {

    var next_button = document.querySelector('.forminator-button-next'),

    textField = $('input[name="input-field-name"]'),

    form = $('.forminator-custom-form'),

    validator = form.data('validator');

    if (typeof next_button !== 'undefined' && next_button !== null) {

    var handleClick = function (event) {

    $("body").append("<div id=\'loader\' class=\'fullscreen-loader\'><div class=\'loader-spin\'></div></div>");

    event.stopImmediatePropagation();

    event.preventDefault();

    var textValue = textField.val();

    $.ajax({

    url: '<?= get_stylesheet_directory_uri() ?>/your-file.php',

    type: 'POST',

    dataType: 'json',

    data: { text: textValue },

    success: function (response) {

    if (response.valid === false) {

    $("#loader").remove();

    validator.invalid[textField.attr('name')] = true;

    validator.showErrors({

    [textField.attr('name')]: response.message

    });

    textField.focus();

    form.data('forminator-invalid', true);

    } else {

    $("#loader").remove();

    delete validator.invalid[textField.attr('name')];

    validator.showErrors({

    [textField.attr('name')]: ""

    });

    form.data('forminator-invalid', false);

    next_button.removeEventListener('click', handleClick, true);

    next_button.click();

    setTimeout(function () {

    next_button.addEventListener('click', handleClick, true);

    }, 0);

    }

    },

    error: function () {

    $("#loader").remove();

    alert('Error.');

    }

    });

    };

    next_button.addEventListener('click', handleClick, true);

    }

    });

    });

    })(jQuery);

    </script>

    <?php

    },

    999

    );

    In the php file i just return false or true and a custom error message.

    Hope this may help someone in the future.

    Alessandro

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

The topic ‘Form Step Submit Hook?’ is closed to new replies.