Skip to content

Numeric Rules

Numeric validation rules check numeric values and their constraints. These rules handle both actual numbers and numeric strings from form submissions.

RuleParametersExampleError Message
numeric$v->rule('numeric', 'field'){field} must be numeric
integerstrict?$v->rule('integer', 'field'){field} must be an integer
minmin$v->rule('min', 'field', 10){field} must be at least {0}
maxmax$v->rule('max', 'field', 100){field} must be at most {0}
betweenmin, 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
decimalPlacesplaces$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.

The strict parameter rejects redundant plus signs:

$v->rule('integer', 'age'); // accepts "+27"
$v->rule('integer', 'age', true); // rejects "+27"

Valicomb supports two syntax styles: rule-based (map rules to fields) and field-based (map fields to rules).

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();
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();