NumericRule
in package
Business range rules for number fields typed as text (prices, rates, quantities in a multiplier row).
The column gate (ColumnGate::decimal()) checks only what the column can hold; a VAT rate of
250 % or a discount of 150 % fits decimal(5,2) and was saved, and a quantity 0 produced a zero line.
These rules state the business range instead.
The value is read like everywhere else in the forms (decimal comma or dot, thousands spaces,
Decimal::clean()); exponent notation is not a number here. Compared with bcmath, never as float.
An empty optional field passes; the caller's setRequired() enforces filling.
The rules are closures, so Nette does not export them to the client (netteForms.js does not know
them); the browser hint stays the min/max HTML attributes the callers set on number inputs.
IMPORTANT: messages are global keys with the // prefix (see ColumnGate), in
admin.general.numericRule.*. %s placeholders are filled from the rule argument.
Table of Contents
Constants
- MIN_MESSAGE : string = '//admin.general.numericRule.min'
- Message for a value below the lower bound (or not a number); `%s` is the inclusive bound.
- NON_ZERO_MESSAGE : string = '//admin.general.numericRule.nonZero'
- Message for a zero (or not a number) where a non-zero number is required.
- RANGE_MESSAGE : string = '//admin.general.numericRule.range'
- Message for a value outside the range; `%s` and `%s` are the inclusive bounds.
- NUMBER_PATTERN : string = '~^-?\d+(\.\d+)?$~D'
- Plain decimal notation after {@see Decimal::clean()}; no exponent, no sign other than a leading minus.
- SCALE : int = 6
Methods
- atLeast() : BaseControl
- Allows only a number of at least `$min` (no upper bound of its own; the column gate adds one).
- isNonZero() : bool
- Tells whether the value is a number other than zero.
- isWithin() : bool
- Tells whether the value is a number within `[$min, $max]`; public for the server-side payload checks that must apply the same range as the form.
- nonZero() : BaseControl
- Allows only a number other than zero (negative numbers pass).
- range() : BaseControl
- Allows only a number within `[$min, $max]` inclusive.
- display() : string
- Bound as shown in the message: `-100`, `0.01` with a decimal comma would need the locale, so dot form stays.
- isEmpty() : bool
- number() : numeric-string|null
Constants
MIN_MESSAGE
Message for a value below the lower bound (or not a number); `%s` is the inclusive bound.
public
string
MIN_MESSAGE
= '//admin.general.numericRule.min'
NON_ZERO_MESSAGE
Message for a zero (or not a number) where a non-zero number is required.
public
string
NON_ZERO_MESSAGE
= '//admin.general.numericRule.nonZero'
RANGE_MESSAGE
Message for a value outside the range; `%s` and `%s` are the inclusive bounds.
public
string
RANGE_MESSAGE
= '//admin.general.numericRule.range'
NUMBER_PATTERN
Plain decimal notation after {@see Decimal::clean()}; no exponent, no sign other than a leading minus.
private
string
NUMBER_PATTERN
= '~^-?\d+(\.\d+)?$~D'
SCALE
private
int
SCALE
= 6
Methods
atLeast()
Allows only a number of at least `$min` (no upper bound of its own; the column gate adds one).
public
static atLeast(BaseControl $control, numeric-string $min[, string $message = self::MIN_MESSAGE ]) : BaseControl
Put it before ColumnGate::decimal(): Nette stops at the first failed rule, so text like abc
gets this field's own message instead of the column range one.
Parameters
- $control : BaseControl
- $min : numeric-string
- $message : string = self::MIN_MESSAGE
Return values
BaseControlisNonZero()
Tells whether the value is a number other than zero.
public
static isNonZero(mixed $value) : bool
Parameters
- $value : mixed
Return values
boolisWithin()
Tells whether the value is a number within `[$min, $max]`; public for the server-side payload checks that must apply the same range as the form.
public
static isWithin(mixed $value, numeric-string $min, numeric-string $max) : bool
Parameters
- $value : mixed
- $min : numeric-string
- $max : numeric-string
Return values
boolnonZero()
Allows only a number other than zero (negative numbers pass).
public
static nonZero(BaseControl $control[, string $message = self::NON_ZERO_MESSAGE ]) : BaseControl
Parameters
- $control : BaseControl
- $message : string = self::NON_ZERO_MESSAGE
Return values
BaseControlrange()
Allows only a number within `[$min, $max]` inclusive.
public
static range(BaseControl $control, numeric-string $min, numeric-string $max[, string $message = self::RANGE_MESSAGE ]) : BaseControl
Parameters
- $control : BaseControl
- $min : numeric-string
- $max : numeric-string
- $message : string = self::RANGE_MESSAGE
Return values
BaseControldisplay()
Bound as shown in the message: `-100`, `0.01` with a decimal comma would need the locale, so dot form stays.
private
static display(string $bound) : string
Parameters
- $bound : string
Return values
stringisEmpty()
private
static isEmpty(mixed $value) : bool
Parameters
- $value : mixed
Return values
boolnumber()
private
static number(mixed $value) : numeric-string|null
Parameters
- $value : mixed