YourPlugins

Add advanced validation rules

Part of Developer reference.

All docs

You can add custom validation rules in PHP by using the calculator error filter.

For new integrations, use yps_calcsheet_filter_calculate_price_errors. The legacy awspc_filter_calculate_price_errors name is still supported for backward compatibility, but it should not be used in new code.

You can place the example below in your theme functions.php file or in a custom plugin. If you prefer, you can also use a snippets plugin.

function custom_yps_calcsheet_filter_calculate_price_errors($errors, $params){
    $calculator = $params['calculator'];
    $userData = $params['userData'];

    /* Apply the rule only to calculator ID 6. */
    if ((int) $calculator->id === 6) {
        $field1 = isset($userData['aws_price_calc_1']) ? (float) $userData['aws_price_calc_1'] : 0;
        $field2 = isset($userData['aws_price_calc_2']) ? (float) $userData['aws_price_calc_2'] : 0;

        if ($field1 > 100 && $field2 > 100) {
            $errorMessage = 'Field1 must be < 100 and Field2 must be < 100';

            $errors['aws_price_calc_1'][] = $errorMessage;
            $errors['aws_price_calc_2'][] = $errorMessage;
        }
    }

    return $errors;
}

add_filter('yps_calcsheet_filter_calculate_price_errors', 'custom_yps_calcsheet_filter_calculate_price_errors', 10, 2);

$params is a PHP array with the following values:

  • $params['errors']: Current validation errors.
  • $params['priceRaw']: The raw calculated price before formatting.
  • $params['product']: The product data.
  • $params['calculator']: The calculator object.
  • $params['data']: Raw submitted values.
  • $params['userData']: Transformed values used by the calculation engine.
  • $params['outputResults']: Calculated output field values.
  • $params['formatPrice']: Whether the final price should be formatted.