function custom_form_generate_validation($invoice_type_id)
{
    global $wpdb;
    // get form fields to validate
    $results = $wpdb->get_results($wpdb->prepare("SELECT * FROM wp_invoice_info where invoice_type_id = %d ORDER BY field_order ASC", $invoice_type_id));
    $error_msg .= validate_emails();
    /* ********** BEGIN Validate form fields */
    foreach ($results as $row2) {
        $attributesArray = unserialize($row2->attributes);
        // nametracking is different that means we have a new field...we only care about non duplicate entries
        if ($nametracking != $row2->field_name) {
            if ($_POST[$row2->field_name] != "") {
                // use != "" instead of == 0 because sometimes 0 is a valid field value
                switch ($row2->field_type) {
                    // check to see if the posted value passes validation
                    case "textbox":
                        $error_msg .= validateTextbox($row2);
                        break;
                    default:
                        // rest don't need validation
                        break;
                }
                // switch
            } elseif (array_key_exists("required", $attributesArray)) {
                $error_msg .= '<p class="cstem_im_form_error">Form field "' . $row2->field_description . '" must not be empty</p>';
            }
        }
        // if: it is a new field so need to validate
        $nametracking = $row2->field_name;
        $prevtype = $row2->field_type;
    }
    // for: look through results
    /* END Validate form fields ********** */
    return $error_msg;
}
function custom_form_generate_validation($results)
{
    global $wpdb;
    // if there is form data coming in then validate the fields
    if ($_POST) {
        /* validate emails */
        $is_checked = $_POST["others_behalf"];
        // check if user is submitted for someone else
        // check how many people user is submitting for
        $num_people = 1;
        if ($is_checked) {
            $num_people = $_POST["num_registering"];
        }
        // validate user's email
        $user_id = $wpdb->get_var($wpdb->prepare("SELECT `ID` FROM `wp_users` WHERE `user_email` = %s;", $_POST["email"]));
        if (!$user_id) {
            $error_msg .= '<p class="cstem_im_form_error">User with email "' . $_POST["email"] . '" (email field 1) does not exist. Check the spelling and make sure that person is registered for the site.</p>';
        }
        // validate others' emails
        for ($email_index = 1; $email_index <= $num_people - 1; $email_index++) {
            $email_name = "email" . $email_index;
            $email = $_POST[$email_name];
            $user_id = $wpdb->get_var($wpdb->prepare("SELECT `ID` FROM `wp_users` WHERE `user_email` = %s;", $email));
            $email_field_num = $email_index + 1;
            if (!$user_id) {
                $error_msg .= '<p class="cstem_im_form_error">User with email "' . $email . '" (email field ' . $email_field_num . ') does not exist. Check the spelling and make sure that person is registered for the site.</p>';
            }
        }
        // for: validate each email
        /* Vaildate form fields */
        foreach ($results as $row2) {
            $attributesArray = unserialize($row2->attributes);
            // nametracking is different that means we have a new field...we only care about non duplicate entries
            if ($nametracking != $row2->field_name) {
                if ($_POST[$row2->field_name] != "") {
                    // use != "" instead of == 0 because sometimes 0 is a valid field value
                    switch ($row2->field_type) {
                        // check to see if the posted value passes validation
                        case "dropdown":
                            // no dropdown specific validation needed
                            break;
                        case "checkbox":
                            // no checkbox specific validation needed
                            break;
                        case "radio":
                            // no radio specific validation needed
                            break;
                        case "textbox":
                            $error_msg .= validateTextbox($row2);
                            break;
                        default:
                            // textfield, textfieldTitle, and textboxDisabled don't need validation (don't get POSTed)
                            break;
                    }
                    // switch
                } elseif (array_key_exists("required", $attributesArray)) {
                    // && $row2->field_type != "textbox")
                    $error_msg .= '<p class="cstem_im_form_error">' . $row2->field_description . ' must not be empty</p>';
                }
            }
            // if: it is a new field so need to validate
            $nametracking = $row2->field_name;
            $prevtype = $row2->field_type;
        }
        // for: look through results
    }
    // if: check if data was posted
    return $error_msg;
}