/** * WPComment Validate * @Since version 24.2 * By: Najeeb Ahmad * Date: January 19, 2022 **/ /*global jQuery wpcomment_get_element_value wpcomment_input_vars*/ const WPComment_Validate = { field_meta: wpcomment_input_vars.field_meta.filter(f => f.required === "on" ), passed: true, $wpcomment_input: jQuery('.wpcomment-input.wpcomment-required'), $wpcomment_input_texts: jQuery('.wpcomment-input.wpcomment-required.text, .wpcomment-input.wpcomment-required.email, .wpcomment-input.wpcomment-required.number,.wpcomment-input.wpcomment-required.quantityoption'), init : async () => { jQuery(document).bind('wpcomment_uploaded_file_removed', async function(e) { WPComment_Validate.validate_data() .then(WPComment_Validate.enable_button, WPComment_Validate.disable_button); }); jQuery(document).bind('wpcomment_field_shown', async function(e) { WPComment_Validate.validate_data() .then(WPComment_Validate.enable_button, WPComment_Validate.disable_button); }); jQuery(document).bind('wpcomment_field_hidden', async function(e) { WPComment_Validate.validate_data() .then(WPComment_Validate.enable_button, WPComment_Validate.disable_button); }); jQuery(document).on('change', WPComment_Validate.$wpcomment_input, async function(e){ WPComment_Validate.validate_data() .then(WPComment_Validate.enable_button, WPComment_Validate.disable_button); }); // keyup events for texts input e.g: text,email,number WPComment_Validate.$wpcomment_input_texts.keyup(async function(e){ WPComment_Validate.validate_data() .then(WPComment_Validate.enable_button, WPComment_Validate.disable_button); }); WPComment_Validate.validate_data() .then(WPComment_Validate.enable_button, WPComment_Validate.disable_button); }, validate_data: () => { return new Promise( function(resolve, reject){ // console.log(WPComment_Validate.field_meta); const invalid_fields = WPComment_Validate.field_meta.filter(f => !WPComment_Validate.field_has_valid_data(f) && !WPComment_Validate.is_field_hidden(f.data_name)) const validate_result = invalid_fields.length > 0 ? false : true; return validate_result ? resolve() : reject(invalid_fields); }); }, is_field_hidden: (data_name) => { // console.log(data_name, jQuery(`.wpcomment-field-wrapper.${data_name}.wpcomment-c-hide`).length > 0); return jQuery(`.wpcomment-field-wrapper.${data_name}.wpcomment-c-hide`).length > 0; }, /** * When a variation is hidden. */ disable_button: (invalid_fields) => { const $form = jQuery('form.comment-form'); $form .find( 'input[type="submit"]' ) .prop('disabled', true); // console.log('disabled', invalid_fields); WPComment_Validate.show_errors(invalid_fields); }, enable_button: () => { const $form = jQuery('form.comment-form'); $form .find( 'input[type="submit"]' ) .prop('disabled', false); //hide error jQuery('#wpcomment-error-container').html(''); }, show_errors: (invalid_fields) => { // console.log(invalid_fields); const $container = jQuery('#wpcomment-error-container').html(''); const $ul_container = jQuery('