Support

Account

Home Forums ACF PRO Multiple validation rules for multiple fields Reply To: Multiple validation rules for multiple fields

  • I set 2 validation rules like this.

    RULE 1
    Complete at least one of field(species1 or species2)
    RULE 2
    Complete at least one of field(price1 or price2 or price3)

    My code works fine as long as only one error. I mean, when both rules have an error, it does not hide error message however I corrected one of errors. It keeps showing error message. It does not do “return $valid”.

    Im wondering if there is way to set multiple validation rules for multiple fields.

    //RULE 1
    function validate_species( $valid, $value, $field, $input ){
        if( !$valid ) {
            return $valid;
        }
    
        $species1 = $_POST['acf']['field_609b650e953ec'];
        $species2 = $_POST['acf']['field_609c4053b0ca8'];
    
        if (empty($species1) && empty($species2))  {
            $valid = 'Error message for Rule1';
        }
    	return $valid;
    }
    add_filter('acf/validate_value/name=species1', 'validate_species', 10, 4);
    add_filter('acf/validate_value/name=species2', 'validate_species', 10, 4);
    
    //RULE 2
    function validate_price( $valid, $value, $field, $input ){
    
        if( !$valid ) {
            return $valid;
        }
    
        $price1 = $_POST['acf']['field_609de26de0b75'];
        $price2 = $_POST['acf']['field_609de2b939a27'];
        $price3 = $_POST['acf']['field_609de324a1398'];
    
        if (empty($price1) && empty($price2) && empty($price3))  {
            $valid = 'Error message for Rule 2';
        }
    
    	return $valid;
    }
    add_filter('acf/validate_value/name=price', 'validate_price', 10, 4);
    add_filter('acf/validate_value/name=tokka', 'validate_price', 10, 4);
    add_filter('acf/validate_value/name=jissai', 'validate_price', 10, 4);