• Hi, I’m trying to do some custom validation of a series of exclusive checkboxes using the wpcf7_validate_checkbox filter, however the challenge is that I am using the pipe character method to have a different display value from the value which is sent via the email. For example, here is the checkbox shortcode:

    [checkbox myoptions use_label_element "This is option One|1" "This is option Two|2" "This is option Three|3"]

    So the resulting Checkbox will show the “This is option X” text but when the email is sent the default value will be the numeric value, e.g. “1”.

    The issue is that I want to create custom validation on the checkboxes to see which option has been selected, but when the form is submitted it is the “This is option X” text which is the $_POST value sent whereas I really want to do a comparison on the numeric value.

    So my question is if there anyway to access the right-hand numeric value during the wpcf7_validate_checkbox validation? Here’s a simplified version of my validation code:

    if ( 'myoptions' == $tag->name ) {
    if ( $_POST[ 'myoptions' ] == '2' ) {
    $result->invalidate( $tag, "Option 2 selected" );
    }
    }

    So basically I want to be able to check if option 1, 2 or 3 was selected, but I am only ably to check if the posted value equals the text, e.g.

    if ( $_POST[ 'myoptions' ] == 'This is option Two' ).

    The problem with matching the text is if that the text is changed in the form then I would have to update the validation code to match, also in my case the text labels are quite lengthy so the difference between matching a long string and a single digit is significant.

    (Side note: this issue would be avoidable if it was possible to set separate value and label text values on checkboxes, but they currently can only be the same text. If not for this restriction in CF7 I would set the label to the text and the value of the checkbox to 1, 2, 3 etc)

    • This topic was modified 1 year, 6 months ago by Rick Curran. Reason: Fixing formatting
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Takayuki Miyoshi

    (@takayukister)

    Basically, user input validation should be done against the raw data in $_POST (Accessing user input data).

    Since a WPCF7_FormTag instance has the pipes property (a WPCF7_Pipes object), you can access the “after pipe” value by the following way:

    $after_pipe = $tag->pipes->do_pipe( $_POST['myoptions'] );

    But, you’ll still need to use the “before pipe” value for the comparison.

    this issue would be avoidable if it was possible to set separate value and label text values on checkboxes

    True, and it is possible if you implement your own custom form-tag that outputs HTML like that.

    Thread Starter Rick Curran

    (@rickcurran)

    @takayukister Ok, thanks for that information!

    Had same situation where codes remain static but text can change + it’s multilingual so keeping all that text in validation would be alot. Thanks for $after_pipe = $tag->pipes->do_pipe( $_POST[‘myoptions’] ); did the trick, here is sample code for reference:

    add_filter( 'wpcf7_validate_select', 'validate_codes', 10, 2 );
    add_filter( 'wpcf7_validate_select*', 'validate_codes', 10, 2 );
    
    function validate_codes( $result, $tag ) {
    
    	$name         = $tag->name;
            //sample values.
    	$valid_values = array(
    		'sd',
    		'sdf',
    		'sdf',
    		'sdf',
    		'sdf',
    		'sdfsd',
    		'345',
    		'345',
    		'gdg',
    		'345',
    		'887',
    		'55',
    		'88',
    		'54',
    		'55',
    		'222',
    		'456',
    	);
    
    	if ( $name == 'myinput' ) {
    
    		$value = isset( $_POST[ $name ] ) ? trim( $_POST[ $name ] ) : '';
    		$after_pipe = $tag->pipes->do_pipe( $value );
    
    		if ( $tag->is_required() && '' === $value ) {
    			$result->invalidate( $tag, '' );
    		}
    
    		$toarray = explode( ',', $after_pipe );
    
    		foreach ( $toarray as $item ) {
    
    			if ( ! in_array( $item, $valid_values, true ) ) {
    
    				$result->invalidate( $tag, $item );
    
    			}
    
    		}
    
    	}
    
    	return $result;
    
    }

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Check raw value of checkbox during validation with wpcf7_validate_checkbox’ is closed to new replies.