JS Error with NinjaForms Uploads Field and hCpatcha field

[ad_1]

Hello,

Thank you for reporting. You can take this code, and save as assets/js/hcaptcha-nf.js and assets/js/hcaptcha-nf.min.js files, replacing them.

/**
* Ninja Forms controller file.
*/

/* global hcaptcha, Marionette, Backbone */

wp.hooks.addFilter(
'hcaptcha.ajaxSubmitButton',
'hcaptcha',
( isAjaxSubmitButton, submitButtonElement ) => {
if ( submitButtonElement.classList.contains( 'nf-element' ) ) {
return true;
}

return isAjaxSubmitButton;
}
);

document.addEventListener( 'DOMContentLoaded', function() {
const HCaptchaFieldController = Marionette.Object.extend( {
initialize() {
// On the Form Submission's field validation.
const submitChannel = Backbone.Radio.channel( 'submit' );
this.listenTo( submitChannel, 'validate:field', this.updateHcaptcha );
this.listenTo( submitChannel, 'validate:field', this.updateHcaptcha );

// On the Field's model value change.
const fieldsChannel = Backbone.Radio.channel( 'fields' );
this.listenTo( fieldsChannel, 'change:modelValue', this.updateHcaptcha );
},

updateHcaptcha( model ) {
// Only validate a specific fields type.
if ( 'hcaptcha-for-ninja-forms' !== model.get( 'type' ) ) {
return;
}

// Check if the Model has a value.
if ( model.get( 'value' ) ) {
// Remove Error from Model.
Backbone.Radio.channel( 'fields' ).request(
'remove:error',
model.get( 'id' ),
'required-error'
);
} else {
const fieldId = model.get( 'id' );
const widget = document.querySelector( '.h-captcha[data-fieldId="' + fieldId + '"] iframe' );

if ( ! widget ) {
return;
}

const widgetId = widget.dataset.hcaptchaWidgetId;
const hcapResponse = hcaptcha.getResponse( widgetId );
model.set( 'value', hcapResponse );
}
},
} );

// Instantiate our custom field's controller, defined above.
window.hCaptchaFieldController = new HCaptchaFieldController();
} );

/* global jQuery */

( function( $ ) {
// noinspection JSCheckFunctionSignatures
$.ajaxPrefilter( function( options ) {
const data = options.data ?? '';

if ( ! ( typeof data === 'string' || data instanceof String ) ) {
return;
}

if ( ! data.startsWith( 'action=nf_ajax_submit' ) ) {
return;
}

const urlParams = new URLSearchParams( data );
const formId = JSON.parse( urlParams.get( 'formData' ) ).id;
const $form = $( '#nf-form-' + formId + '-cont' );
let id = $form.find( '[name="hcaptcha-widget-id"]' ).val();
id = id ? id : '';
options.data += '&hcaptcha-widget-id=' + id;
} );
}( jQuery ) );

I have added the fix to the v4.4.0, which will be released next month.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer