• Resolved StevenP94

    (@stevenp94)


    Hello,
    I’m in trouble with the wp gdpr compliance plugin that add a checkbox to CF7 forms to ensure privacy compliance.
    This checkbox should be required, but I received some mails from forms that reports this control unchecked.

    After a lot of investigations, I replicated the problem on a test site and with custom written plugin code to make tests.

    In few words, if all the controls are shown all work as expected, but if I hide a group the $result->invalidate($tag, $message) is ignored.

    In addition all the internal validation works, only the custom one is ignored.

    Please help me: the forms without privacy acceptance are a big problem!

Viewing 15 replies - 1 through 15 (of 23 total)
  • Plugin Author Jules Colle

    (@jules-colle)

    Can you switch to using an acceptance field instead? https://contactform7.com/acceptance-checkbox/
    It’s the builtin control to make sure that privacy options are checked. So you don’t need a separate plugin to do this.

    Do note that, because the acceptance field is required and validated in a more forcing manner, you cannot submit a form where the field is left unchecked inside a hidden group. So you need to make sure that the acceptance field is not inside any group. The best place to put it would be right before the submit button, or at the very beginning of you form.

    Thread Starter StevenP94

    (@stevenp94)

    Thanks for your kind reply.

    I’m sorry but I can’t remove that plugin:
    I have more than 100 contact forms and the wp gdpr compliance plugin manage other aspects of privacy and cookies in addition to the acceptance check.

    The shortcut that add this control is just before the submit and is not inside a group, and it works like the acceptance field. The problem is that it uses custom validation that doesn’t work when there is an hidden group (anywhere in the form)

    I made a test site for this, and I can give you access if you need.

    I apologize for my english

    • This reply was modified 2 years, 6 months ago by StevenP94.
    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    I made a test site for this, and I can give you access if you need.

    If @jules-colle does take you up on that offer, I’ll remove their access to the forums.

    *DRINKS COFFEE*

    Not that Jules would, he does know better. ??

    Please do not offer anyone access like that even to a demo site. That is going too far and is not only dangerous for you it is also a real liability risk for Jules.

    Please work here using your words to solve your problem.

    Thread Starter StevenP94

    (@stevenp94)

    @jdembowski thanks for your advice, I missed that. Sorry for offering that.

    Plugin Author Jules Colle

    (@jules-colle)

    @stevenp94 you could set up the form here if you want: https://conditional-fields-cf7.bdwm.be/form-tester/

    Can you also let me know which plugin you are using, so I can temporarily enable it on my site.

    Thread Starter StevenP94

    (@stevenp94)

    This is a test form:

    https://conditional-fields-cf7.bdwm.be/form-tester/?hash=732ed3af6d6cc913570a5d0279fd0672

    Nothing special, just a test form made using the standard contact form example module.

    the [wpgdprc] tag is the shortcode added by

    Cookie Information | Free GDPR Consent Solution

    this plugins adds the tag in the form and at runtime is replaced by a required checkbox.

    If all is shown, all works fine and the validation of required checkbox works.
    If I hide a group (just the default condition in my example) the checkbox is not required anymore.

    Hope that this can help.

    P.S. I have the same problem if I make custom code to do that using:

    wpcf7_add_form_tag( 'wpgdprc', 'addFormTagHandler' );
    add_filter( 'wpcf7_validate_wpgdprc','myvalidation',10,2)

    the invalidate is executed but has no effect
    $result->invalidate( $tag, 'this is required field' );

    • This reply was modified 2 years, 6 months ago by StevenP94.
    • This reply was modified 2 years, 6 months ago by StevenP94.
    Plugin Author Jules Colle

    (@jules-colle)

    Hmm, this indeed doesn’t seem to work. I would say this is a problem with the GDPR plugin though. As far as I know my plugin is following best practices, and any custom CF7 form tags that are registered correctly should play nice with it.

    That said, I do notice that it’s impossible to submit the form without the checkbox being checked. So you are 100% sure that people have checked the box when the form is submitted. Therefore you could just add the “checkbox accepted” text manually in the email because it will always be true anyway.

    Thread Starter StevenP94

    (@stevenp94)

    The problem is not the mail, that part works.
    The problem is that the validation on thecheckbox is bypassed by hidden groups.

    I can give you a simple plugin code that I wrote to make tests, maybe you can help me to see where is wrong and why is bypassed by hidden groups.

    Just let me know if is possible and how to give you the code. Hop that this don’t break any rules.

    Regards.

    Plugin Author Jules Colle

    (@jules-colle)

    You can paste code snippets here. You don’t break any rules with that ??
    Just make sure that you wrap your code with backticks so it looks like this

    And obviously make sure that you are not including any absolute paths, passwords or any other information that could be considered security risks

    • This reply was modified 2 years, 6 months ago by Jules Colle.
    • This reply was modified 2 years, 6 months ago by Jules Colle.
    Thread Starter StevenP94

    (@stevenp94)

    <?php
    /**
    * Plugin Name: My Plugin
    * Plugin URI: https://www.yourwebsiteurl.com/
    * Description: This is the very first plugin I ever created.
    * Version: 1.0
    * Author: Steven
    * Author URI: https://yourwebsiteurl.com/
    **/
    
    // this code is an extract of wpgdprc plugin to make tests on CF7 validation
    
    add_action( 'wpcf7_init', 'addFormTagSupport' );
    add_filter( 'wpcf7_validate_wpgdprc', 'validateField', 10, 2 );
    
    	function addFormTagSupport() {
    		if ( ! function_exists( 'wpcf7_add_form_tag' ) ) {
    			return;
    		}
    		wpcf7_add_form_tag( getFieldTag(), 'addFormTagHandler' );
    	}
    
    	/**
    	 * @param mixed $tag (array|WPCF7_FormTag)
    	 *
    	 * @return string
    	 */
    	function addFormTagHandler( $tag = [] ): string {
    		$tag = validateFormTag( $tag );
    		if ( empty( $tag ) ) {
    			return '';
    		}
    
    		$tag->name   = getFieldTag();
    		$first_label = reset( $tag->labels );
    		$label       = ! empty( $first_label ) ? esc_html( $first_label ) : getCheckboxTextByForm();
    
    		$class     = [ wpcf7_form_controls_class( $tag->type, 'wpcf7-validates-as-required' ) ];
    		$has_error = wpcf7_get_validation_error( $tag->name );
    		if ( $has_error ) {
    			$class[] = 'wpcf7-not-valid';
    		}
    
    		$field_atts = [
    			'type'          => 'checkbox',
    			'name'          => $tag->name,
    			'value'         => 1,
    			'tabindex'      => $tag->get_option( 'tabindex', 'signed_int', true ),
    			'aria-required' => 'true',
    			'aria-invalid'  => $has_error ? 'true' : 'false',
    		];
    
    		$field = [ '<input %2$s />', '<span class="wpcf7-list-item-label">%1$s</span>' ];
    		if ( $tag->has_option( 'label_first' ) ) {
    			$field = array_reverse( $field );
    		}
    		$output = sprintf( implode( '', $field ), esc_html( $label ), wpcf7_format_atts( $field_atts ) );
    
    		if ( $tag->has_option( 'use_label_element' ) ) {
    			$output = '<label>' . $output . '</label>';
    		}
    
    		$wrapper_atts = [
    			'class' => $tag->get_class_option( implode( ' ', $class ) ),
    			'id'    => $tag->get_id_option(),
    		];
    
    		return sprintf(
    			'<span class="wpcf7-form-control-wrap %1s"><span %2s>%3s</span>%4s</span>',
    			sanitize_html_class( $tag->name ),
    			wpcf7_format_atts( $wrapper_atts ),
    			'<span class="wpcf7-list-item">' . $output . '</span>',
    			$has_error
    		);
    	}
    
    	/**
    	 * Allows for wrapping the field tag (like in the CF form & email)
    	 *
    	 * @param bool $wrapped
    	 *
    	 * @return string
    	 */
    	function getFieldTag( bool $wrapped = false ): string {
    		$tag = 'wpgdprc';
    
    		return $wrapped ? '[' . $tag . ']' : $tag;
    	}
    
    	/**
    	 * Gets checkbox text for specific form
    	 *
    	 * @param int|string $form_id
    	 *
    	 * @return string
    	 */
    	function getCheckboxTextByForm( $form_id = 0 ): string {
    		return 'testo aggiunto al check';
    	}
    
    	/**
    	 * Validates form tag (and checks if it is the WPGDRPC form tag)
    	 *
    	 * @param mixed $tag (array|WPCF7_FormTag)
    	 *
    	 * @return mixed (false|WPCF7_FormTag)
    	 */
    	function validateFormTag( $tag = [] ) {
    		if ( ! class_exists( 'WPCF7_FormTag' ) ) {
    			return false;
    		}
    
    		$tag = is_array( $tag ) ? new \WPCF7_FormTag( $tag ) : $tag;
    		if ( $tag->type !== getFieldTag() ) {
    			return false;
    		}
    
    		return $tag;
    	}
    
    	/**
    	 * @param mixed $result
    	 * @param mixed $tag (array|WPCF7_FormTag)
    	 *
    	 * @return mixed (WPCF7_Validation)
    	 */
    	function validateField( $result, $tag = [] ) {
    		if ( ! class_exists( 'WPCF7_Validation' ) ) {
    			return $result;
    		}
    		if ( ! $result instanceof \WPCF7_Validation ) {
    			return $result;
    		}
    
    		$tag = validateFormTag( $tag );
    		if ( empty( $tag ) ) {
    			return $result;
    		}
    
    		$value = false;
    		if ( ! empty( $_POST[ getFieldTag() ] ) ) {
    			$value = filter_var( wp_unslash( $_POST[ getFieldTag() ] ), FILTER_VALIDATE_BOOLEAN );
    		}
    		if ( ! empty( $value ) ) {
    			return $result;
    		}
    
    		$key     = '_wpcf7';
    		$form_id = ! empty( $_POST[ $key ] ) && is_numeric( $_POST[ $key ] ) ? (int) $_POST[ $key ] : 0;
    		if ( empty( $form_id ) ) {
    			return $result;
    		}
    
    		$tag->name = getFieldTag();
    		$result->invalidate( $tag,  'You must fill the checkbox!' );
    
    		return $result;
    	}
    Thread Starter StevenP94

    (@stevenp94)

    Done, just waiting for moderation.

    Plugin Author Jules Colle

    (@jules-colle)

    I installed your plugin on my demo website (and I uninstalled the GDPR plugin), and seems to work as expected. see https://conditional-fields-cf7.bdwm.be/form-tester/?hash=a99822744da6826b0abc3a4f8584bdd8

    Thread Starter StevenP94

    (@stevenp94)

    Yes, Jules, I see, but my original test form still not working:

    https://conditional-fields-cf7.bdwm.be/form-tester/?hash=d3d4a571de454dcae614fc3e56100ec7

    maybe are the clear_on_hide or the inline attributes, I’m making some test on that

    Thread Starter StevenP94

    (@stevenp94)

    removing the attributes, still not working – so weird!

    Plugin Author Jules Colle

    (@jules-colle)

    I’m unable to submit your original form when i leave the checkbox unchecked, so that works as expected..

Viewing 15 replies - 1 through 15 (of 23 total)
  • The topic ‘Custom fields validation doesn’t work if a group is hidden’ is closed to new replies.