abozzi
Forum Replies Created
-
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
Hello @wpmudev-support7 ,
Thank you for your response!
I’ve tried using the
forminator_pagination_step_markuphook to validate thetext-1input 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,
AlessandroHi @wpmudev-support8 ,
i tried using
forminator_pagination_step_markupbut 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,
AlessandroHi 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_markupis the right choice?Best Regards,
Alessandroadd_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.
Hi,
Thank you for your prompt response and for looking into this issue. I appreciate you passing it on to your QA Team for further testing.
I’ll wait for your update and I hope my report was helpful.
Thanks again for your assistance!
Best regards,
Alessandro