Viewing 3 replies - 1 through 3 (of 3 total)
  • Not sure if it will work but it worth the try.

    In the before_send_mail hook, catch the values and verify if A + B < 180
    If not, define a value of 999 to A and 999 to B.

    Add this function in function.php (‘tag-A’ must be the name of your tag)

    add_filter( 'wpcf7_validate_number*', 'tag-A-validation', 20, 2 );
    
    function tag-A-validation( $result, $tag ) {
    	$tag = new WPCF7_Shortcode( $tag );
    
    	if ($tag->name == 'tag-A'){
    		$tagA = $_POST['tag-A'];
    		if ( $tagA == 999 ) {
    			$result->invalidate( $tag, "A + B must be < 180");
    		}
    	}
        return $result;
    }

    Hi there,

    is anybody still on this topic, ’cause I’m trying to figure out the same problem.

    More specifically, the contact form is to enquire about the vacancy of hotel room.
    For each room, I need to know how many people/pets are going to be in that room.
    Of course, there is a limit to it, and that is 4 people/pets.

    I’d like to insert an error message anytime they try to book a room for more than 4 people/pets.

    My last – unsuccessful – attemp was the following code:

    add_filter( 'wpcf7_max_capacity*', 'custom_max_capacity_validation_filter', 20, 2 );
    
    function custom_max_capacity_validation_filter( $result, $tag, $zero, $adults, $children, $babies, $pets ) {
    	$tag = new WPCF7_FormTag( $tag );
    	$zero = "0";
    	if ( 'r1-adults' == $tag->name AND 'r1-children' == $tag->name AND 'r1-babies' == $tag->name AND 'r1-pets' == $tag->name ) {
    	$adults = isset( $_POST['r1-adults'] ) ? settype(trim( $_POST['r1-adults'] ), "integer") : settype($zero, "integer");
    	$children = isset( $_POST['r1-children'] ) ? settype(trim( $_POST['r1-children'] ), "integer") : settype($zero, "integer");
    	$babies = isset( $_POST['r1-babies'] ) ? settype(trim( $_POST['r1-babies'] ), "integer") : settype($zero, "integer");
    	$pets = isset( $_POST['r1-pets'] ) ? settype(trim( $_POST['r1-pets'] ), "integer") : settype($zero, "integer");	
    	$sum=$adults+$children+$babies+$pets;
    		if ($sum > 4 ) {
    			$result->invalidate( $tag, "Max occupancy per room: 4 people/pets" );
    		}
    	}
    	return $result;
    }

    Any idea how to fix it?

    Thanks!

    I solved it. Please look at the last post here: https://wordpress.org/support/topic/sum-validation/#post-8543047

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Validate Sum of 2 Fields’ is closed to new replies.