Numeric Rules
Numeric validation rules check numeric values and their constraints. These rules handle both actual numbers and numeric strings from form submissions.
Rules Reference
Section titled “Rules Reference”| Rule | Parameters | Example | Error Message |
|---|---|---|---|
numeric | — | $v->rule('numeric', 'field') | {field} must be numeric |
integer | strict? | $v->rule('integer', 'field') | {field} must be an integer |
min | min | $v->rule('min', 'field', 10) | {field} must be at least {0} |
max | max | $v->rule('max', 'field', 100) | {field} must be at most {0} |
between | min, max | $v->rule('between', 'field', 1, 10) | {field} must be between {0} and {1} |
boolean | — | $v->rule('boolean', 'field') | {field} must be a boolean |
positive | — | $v->rule('positive', 'field') | {field} must be a positive number |
decimalPlaces | places | $v->rule('decimalPlaces', 'field', 2) | {field} must have at most {0} decimal places |
Note: Parameters marked with
?are optional.
Valid numeric values: integers, floats, numeric strings, negative numbers, scientific notation (1.2e3).
Boolean values: true/false, 1/0, '1'/'0'.
Positive: Must be > 0 (zero is not positive).
BCMath precision: When available, min/max/between use high-precision arithmetic.
Parameter Details
Section titled “Parameter Details”integer
Section titled “integer”The strict parameter rejects redundant plus signs:
$v->rule('integer', 'age'); // accepts "+27"$v->rule('integer', 'age', true); // rejects "+27"Complete Examples
Section titled “Complete Examples”Valicomb supports two syntax styles: rule-based (map rules to fields) and field-based (map fields to rules).
Rule-Based Array Syntax
Section titled “Rule-Based Array Syntax”use Frostybee\Valicomb\Validator;
$v = new Validator($data);
$v->rules([ 'numeric' => [ ['price'], ['tax_rate'] ], 'integer' => [ ['quantity'], ['stock_count'] ], 'min' => [ ['quantity', 1], ['rating', 0] ], 'max' => [ ['quantity', 100], ['rating', 5] ], 'between' => [ ['age', 18, 120], ['percentage', 0, 100] ], 'boolean' => [ ['is_active'], ['is_verified'] ], 'positive' => [ ['price'], ['quantity'] ], 'decimalPlaces' => [ ['price', 2], ['tax_rate', 4] ]]);
$v->validate();Field-Based Mapping
Section titled “Field-Based Mapping”use Frostybee\Valicomb\Validator;
$v = new Validator($data);
$v->mapManyFieldsToRules([ 'price' => ['required', 'numeric', 'positive', ['decimalPlaces', 2]], 'quantity' => ['required', 'integer', ['min', 1], ['max', 100]], 'discount_percent' => ['optional', 'numeric', ['between', 0, 100]], 'rating' => ['required', 'integer', ['between', 1, 5]], 'tax_rate' => ['required', 'numeric', ['decimalPlaces', 4]], 'is_featured' => ['optional', 'boolean'], 'stock_count' => ['required', 'integer', 'positive']]);
$v->validate();