0

I am trying to solve this problem in contact form 7, I have a readonly input field where the value changes according to the sum of other fields. I would like to prevent the submission of the form if the value of this field (id="totalsum") is =! 100

Here is my code:

function totalsum_validation($result,$tag){
    $type = $tag['type'];
    $name = $tag['name'];
    if($name == 'totalsum'){
                $totalsum = isset( $_POST['totalsum'] ) ? trim( $_POST['totalsum'] ) : '';
        $the_value = preg_match("100",$_POST[$name]);
        if($totalsum != "100" || $the_value == false ){ 
            $result->invalidate( $tag, "incorrect value" );
        }
    }
    return $result;
}
add_filter('wpcf7_validate_text','totalsum_validation', 10, 2);
add_filter('wpcf7_validate_text*', 'totalsum_validation', 10, 2);
0

This is not possible with CF7 plugin alone, If you install the Smart Grid extension, it has a filter to validate submissions,

add_filter( 'cf7sg_validate_submission','validate_field_submission',10,3);
function validate_field_submission($validation_errors, $submission, $cf7_key){
  /* $submission an array of <field-name>=>$value pairs one for each submitted field.
  tabbed/tabled sections fields have arrays as $values.
  tables within tabs have aray of array as values.
  if a value is not valid, return a <field-name>=><error message string> pair in the $validation_errors array. The sbumission process will be cancelled and teh user required to correct the field before re-submitting.
  $cf7_key unique form key to identify your form, $cf7_id is its post_id.
  */
  if('my-form'==$cf7_key ){ //check if this is he correct form.
    //$validation_errors is an array of field-names=>error messages.
    //these include the simple validation exposed in the CF7 plugin for required fields/special field formats.
    if(isset($validation_errors['totalsum']) && $submission['totalsum'] < 100){
      $validation_errors['totalsum'] = 'incorrect value!';
    }
  }
  return $validation_errors;
}
0

You would need to know the name of the readonly field to compare against. The Contact Form 7 Documentation on Custom Validation shows that $tag is an object and not an array. An example of what you're trying to do could look like this:

/**
 * Contact Form 7
 * Ensure field [name="total"] is not less than 100
 *
 * @param WPCF7_Validation $result      - Tells CF7 whether to invalidate the form and why.
 * @param WPCF7_FormTag $tag            - The current tag to validate.
 *
 * @return WPCF7_Validation $result
 */
function wpse386451_validate_wpcf7_field_total( $result, $tag ) {

    // If the current tag name isn't what we're looking for return early.
    if( 'total' !== $tag->name ) {
        return $result;
    }

    // Parse the current total to an integer or automatically default to 0 (failure case)
    $total = ( isset( $_POST, $_POST['total'] ) ) ? (int)$_POST['total'] : 0;

    // Value is not 100, invalidate form and provide message for the user.
    if( 100 !== $total ) {
        $result->invalidate( $tag, 'Total cannot be less than 100.' );
    }

    return $result;

}
add_filter( 'wpcf7_validate_text', 'wpse386451_validate_wpcf7_field_total', 10, 2 );
add_filter( 'wpcf7_validate_text*', 'wpse386451_validate_wpcf7_field_total', 10, 2 );

Not the answer you're looking for? Browse other questions tagged or ask your own question.