Final Class Yiisoft\Validator\Rule\Number
Defines validation options to check that the value is a number.
The format of the number must match the regular expression specified in {@see \Yiisoft\Validator\Rule\Number::$pattern}. Optionally, you may configure the {@see \Yiisoft\Validator\Rule\Number::$min} and {@see \Yiisoft\Validator\Rule\Number::$max} to ensure the number is within a certain range.
As an alternative, see also {@see \Yiisoft\Validator\Rule\FloatType} and {@see \Yiisoft\Validator\Rule\IntegerType} rules, that only strictly allow float and integer values accordingly and not the ones that evaluate to such (for example number within a string). Or you can use them together with this rule to limit the valid types.
See also:
Public Methods
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| DEFAULT_GREATER_THAN_MAX_MESSAGE | '{Property} must be no greater than {max}.' | A default for {@see $greaterThanMaxMessage}. | Yiisoft\Validator\Rule\AbstractNumber |
| DEFAULT_INCORRECT_INPUT_MESSAGE | 'The allowed types for {property} are integer, float and ' . 'string. {type} given.' | A default for {@see $incorrectInputMessage}. | Yiisoft\Validator\Rule\AbstractNumber |
| DEFAULT_LESS_THAN_MIN_MESSAGE | '{Property} must be no less than {min}.' | A default for {@see $lessThanMinMessage}. | Yiisoft\Validator\Rule\AbstractNumber |
Method Details
| public mixed __construct ( float|integer|null $min = null, float|integer|null $max = null, string $incorrectInputMessage = 'The allowed types for {property} are integer, float and string. {type} given.', string $notNumberMessage = '{Property} must be a number.', string $lessThanMinMessage = '{Property} must be no less than {min}.', string $greaterThanMaxMessage = '{Property} must be no greater than {max}.', string $pattern = '/^\\s*[-+]?\\d*\\.?\\d+([eE][-+]?\\d+)?\\s*$/', boolean|callable|null $skipOnEmpty = null, boolean $skipOnError = false, Closure|null $when = null ) | ||
| $min | float|integer|null |
Lower limit of the number. Defaults to |
| $max | float|integer|null |
Upper limit of the number. Defaults to |
| $incorrectInputMessage | string |
Error message used when the value is not numeric. You may use the following placeholders in the message:
|
| $notNumberMessage | string |
Error message used when the value does not match {@see $pattern}. You may use the following placeholders in the message:
|
| $lessThanMinMessage | string |
Error message used when the value is smaller than {@link $min}. You may use the following placeholders in the message:
|
| $greaterThanMaxMessage | string |
Error message used when the value is bigger than {@link $max}. You may use the following placeholders in the message:
|
| $pattern | string |
The regular expression for matching numbers. It defaults to a pattern that matches floating numbers with optional exponential part (e.g. -1.23e-10). |
| $skipOnEmpty | boolean|callable|null |
Whether to skip this rule if the value validated is empty. See {@see \Yiisoft\Validator\SkipOnEmptyInterface}. |
| $skipOnError | boolean |
Whether to skip this rule if any of the previous rules gave an error. See {@see \Yiisoft\Validator\Rule\SkipOnErrorInterface}. |
| $when | Closure|null |
A callable to define a condition for applying the rule. See {@see \Yiisoft\Validator\WhenInterface}. |
public function __construct(
float|int|null $min = null,
float|int|null $max = null,
string $incorrectInputMessage = 'The allowed types for {property} are integer, float and string. {type} given.',
string $notNumberMessage = '{Property} must be a number.',
string $lessThanMinMessage = '{Property} must be no less than {min}.',
string $greaterThanMaxMessage = '{Property} must be no greater than {max}.',
string $pattern = '/^\s*[-+]?\d*\.?\d+([eE][-+]?\d+)?\s*$/',
bool|callable|null $skipOnEmpty = null,
bool $skipOnError = false,
?Closure $when = null,
) {
parent::__construct(
min: $min,
max: $max,
incorrectInputMessage: $incorrectInputMessage,
notNumberMessage: $notNumberMessage,
lessThanMinMessage: $lessThanMinMessage,
greaterThanMaxMessage: $greaterThanMaxMessage,
pattern: $pattern,
skipOnEmpty: $skipOnEmpty,
skipOnError: $skipOnError,
when: $when,
);
}
Defined in: Yiisoft\Validator\Rule\AbstractNumber::getGreaterThanMaxMessage()
Get error message used when the value is bigger than $max.
| public string getGreaterThanMaxMessage ( ) | ||
| return | string |
Error message. |
|---|---|---|
public function getGreaterThanMaxMessage(): string
{
return $this->greaterThanMaxMessage;
}
| public string getHandler ( ) |
public function getHandler(): string
{
return NumberHandler::class;
}
Defined in: Yiisoft\Validator\Rule\AbstractNumber::getIncorrectInputMessage()
Get error message used when the value is not numeric.
| public string getIncorrectInputMessage ( ) | ||
| return | string |
Error message. |
|---|---|---|
public function getIncorrectInputMessage(): string
{
return $this->incorrectInputMessage;
}
Defined in: Yiisoft\Validator\Rule\AbstractNumber::getLessThanMinMessage()
Get error message used when the value is smaller than $min.
| public string getLessThanMinMessage ( ) | ||
| return | string |
Error message. |
|---|---|---|
public function getLessThanMinMessage(): string
{
return $this->lessThanMinMessage;
}
Defined in: Yiisoft\Validator\Rule\AbstractNumber::getMax()
Get upper limit of the number. null means no upper limit.
| public float|integer|null getMax ( ) | ||
| return | float|integer|null |
Upper limit of the number. |
|---|---|---|
public function getMax(): float|int|null
{
return $this->max;
}
Defined in: Yiisoft\Validator\Rule\AbstractNumber::getMin()
Get lower limit of the number. null means no lower limit.
| public float|integer|null getMin ( ) | ||
| return | float|integer|null |
Lower limit of the number. |
|---|---|---|
public function getMin(): float|int|null
{
return $this->min;
}
Defined in: Yiisoft\Validator\Rule\AbstractNumber::getName()
| public string getName ( ) |
public function getName(): string
{
return static::class;
}
Defined in: Yiisoft\Validator\Rule\AbstractNumber::getNotNumberMessage()
Get error message used when the value does not match {@see $pattern}
| public string getNotNumberMessage ( ) | ||
| return | string |
Error message. |
|---|---|---|
public function getNotNumberMessage(): string
{
return $this->notNumberMessage;
}
| public array getOptions ( ) |
public function getOptions(): array
{
return [
'min' => $this->min,
'max' => $this->max,
'incorrectInputMessage' => [
'template' => $this->incorrectInputMessage,
'parameters' => [],
],
'notNumberMessage' => [
'template' => $this->notNumberMessage,
'parameters' => [],
],
'lessThanMinMessage' => [
'template' => $this->lessThanMinMessage,
'parameters' => ['min' => $this->min],
],
'greaterThanMaxMessage' => [
'template' => $this->greaterThanMaxMessage,
'parameters' => ['max' => $this->max],
],
'skipOnEmpty' => $this->getSkipOnEmptyOption(),
'skipOnError' => $this->skipOnError,
'pattern' => $this->pattern,
];
}
Defined in: Yiisoft\Validator\Rule\AbstractNumber::getPattern()
The regular expression for matching numbers.
| public string getPattern ( ) | ||
| return | string |
The regular expression. |
|---|---|---|
public function getPattern(): string
{
return $this->pattern;
}
Defined in: Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait::getSkipOnEmpty()
A getter for $skipOnEmpty property.
| public boolean|callable|null getSkipOnEmpty ( ) | ||
| return | boolean|callable|null |
A current raw (non-normalized) value. |
|---|---|---|
public function getSkipOnEmpty(): bool|callable|null
{
return $this->skipOnEmpty;
}
Defined in: Yiisoft\Validator\Rule\Trait\WhenTrait::getWhen()
A getter for $when property.
| public Closure|null getWhen ( ) | ||
| return | Closure|null |
Current value:
|
|---|---|---|
public function getWhen(): ?Closure
{
return $this->when;
}
Defined in: Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait::shouldSkipOnError()
A getter for $skipOnError property.
| public boolean shouldSkipOnError ( ) | ||
| return | boolean |
Current value. |
|---|---|---|
public function shouldSkipOnError(): bool
{
return $this->skipOnError;
}
Defined in: Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait::skipOnEmpty()
An immutable setter to change $skipOnEmpty property.
| public $this skipOnEmpty ( boolean|callable|null $value ) | ||
| $value | boolean|callable|null |
A new value. |
| return | $this |
The new instance with a changed value. |
|---|---|---|
public function skipOnEmpty(bool|callable|null $value): static
{
$new = clone $this;
$new->skipOnEmpty = $value;
return $new;
}
Defined in: Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait::skipOnError()
An immutable setter to change $skipOnError property.
| public $this skipOnError ( boolean $value ) | ||
| $value | boolean |
A new value. |
| return | $this |
The new instance with a changed value. |
|---|---|---|
public function skipOnError(bool $value): static
{
$new = clone $this;
$new->skipOnError = $value;
return $new;
}
Defined in: Yiisoft\Validator\Rule\Trait\WhenTrait::when()
An immutable setter to change $when property.
| public $this when ( Closure|null $value ) | ||
| $value | Closure|null |
A new value:
|
| return | $this |
The new instance with a changed value. |
|---|---|---|
public function when(?Closure $value): static
{
$new = clone $this;
$new->when = $value;
return $new;
}
Signup or Login in order to comment.