Conditional logic validation with the All and Any rules

Posts in the developer's cookbook category describe topics that require technical knowledge. Be warned and use the techniques at your own risk. Unless you are an experienced developer, you are strongly advised to ask a professional for support.

In the Schema-Woven Validation terminology, a composite rule is a rule that has a set of child rules. Contact Form 7 5.9 and later support composite rule types, all and any, with which you can implement conditional logic in user input validation.

The All rule

An all rule verifies that all the child rules in the rules property are verified. The following JSON snippet shows an example of an all rule:

{
    "rule": "all",
    "rules": [
        {
            "rule": "required",
            "field": "adelie",
            "error": "Pick Adélie penguin !"
        },
        {
            "rule": "required",
            "field": "emperor",
            "error": "Pick Emperor penguin !"
        }
    ]
}

Child rules are evaluated in order from the top. If one of the child rules fails, the iteration terminates there, and the processor will not evaluate the remaining child rules. Leveraging this nature of an all rule, you can use a child rule as a conditional statement that will determine if the following child rules should be evaluated.

In cases where there is a child rule that has failed verification, and it has the error and field properties set, those properties will override the parent rule’s counterparts. If both the failed child rule and its parent all rule have no error property set, no validation error message will be displayed.

The Any rule

An any rule verifies that any of the child rules in the rules property are verified. The following JSON snippet shows an example of an any rule:

{
    "rule": "any",
    "field": "humboldt",
    "error": "Pick a penguin !",
    "rules": [
        {
            "rule": "required",
            "field": "gentoo"
        },
        {
            "rule": "required",
            "field": "humboldt"
        }
    ]
}

Child rules are evaluated in order from the top. If one of the child rules passes, the iteration terminates there, and the processor will not evaluate the remaining child rules.

In cases where an any rule fails (none of the child rules pass), and it has the error and field properties set, the error message that the error property specifies will be displayed in the field that the field property specifies.

Implementing a composite rule

To add a composite rule (such as an all or any rule described above) to the validation schema, first, create a rule object as the parent rule using the wpcf7_swv_create_rule() function, and then add child rules using the parent’s add_rule() method.

add_action(
    'wpcf7_swv_create_schema',
    function ( $schema, $contact_form ) {
      
        // All rule
        $all_rule = wpcf7_swv_create_rule( 'all' );

        $all_rule->add_rule(
            wpcf7_swv_create_rule( 'required', array(
                'field' => 'adelie',
                'error' => "Pick Adélie penguin !",
            ) )
        );

        $all_rule->add_rule(
            wpcf7_swv_create_rule( 'required', array(
                'field' => 'emperor',
                'error' => "Pick Emperor penguin !",
            ) )
        );

        // Adding All rule to schema
        $schema->add_rule( $all_rule );

        // Any rule
        $any_rule = wpcf7_swv_create_rule( 'any', array(
            'field' => 'humboldt',
            'error' => "Pick a penguin !",
        ) );

        $any_rule->add_rule(
            wpcf7_swv_create_rule( 'required', array(
                'field' => 'gentoo',
            ) )
        );

        $any_rule->add_rule(
            wpcf7_swv_create_rule( 'required', array(
                'field' => 'humboldt',
            ) )
        );

        // Adding Any rule to schema
        $schema->add_rule( $any_rule );
    },
    10, 2
);