<?php
/*
Plugin Name: Conditional Error Checking
Description: Custom hierarchical error checking rules
Version: 1.00
CMS Version Required: 3.59
*/

// register callbacks
addAction('record_save_errorchecking', 'conditionalErrorChecking', null, 3);

//
function conditionalErrorChecking($tableName, $recordExists, $oldRecord) {

    // skip all but target table/section
    if ($tableName !== 'your_table_here') { return; }

    // conditional error checking rules
    $errors = [];
    $isWarrantyServiced = !empty($_REQUEST['warranty_serviced']);
    if ($isWarrantyServiced) {
        if (empty($_REQUEST['date_of_service:string'])) { // add :string prefix to date entire date string (if set)
            $errors[] = "Please fill out the date of service field.";
        }
        if (empty($_REQUEST['repair_type'])) {
            $errors[] = "Please fill out the repair type field.";
        }
        $wysiwygWithoutTagsOrWhitespace = trim(strip_tags($_REQUEST['description'] ?? ''));
        if (empty($wysiwygWithoutTagsOrWhitespace)) { // wysiwyg, remove all tags and check if empty after that
            $errors[] = "Please fill out the description type field.";
        }
    }

    // show errors
    if ($errors) {
        $errorsHTML = implode("\n", $errors);
        die($errorsHTML);
    }
}
