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 \Yiisoft\Validator\Rule\Number::$pattern. Optionally, you may configure the \Yiisoft\Validator\Rule\Number::$min and \Yiisoft\Validator\Rule\Number::$max to ensure the number is within a certain range.
As an alternative, see also \Yiisoft\Validator\Rule\FloatType and \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 $greaterThanMaxMessage. | Yiisoft\Validator\Rule\AbstractNumber |
| DEFAULT_INCORRECT_INPUT_MESSAGE | 'The allowed types for {property} are integer, float and ' . 'string. {type} given.' | A default for $incorrectInputMessage. | Yiisoft\Validator\Rule\AbstractNumber |
| DEFAULT_LESS_THAN_MIN_MESSAGE | '{Property} must be no less than {min}.' | A default for $lessThanMinMessage. | Yiisoft\Validator\Rule\AbstractNumber |
Method Details
| public __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 ): mixed | ||
| $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 $pattern. You may use the following placeholders in the message:
|
| $lessThanMinMessage | string |
Error message used when the value is smaller than $min. You may use the following placeholders in the message:
|
| $greaterThanMaxMessage | string |
Error message used when the value is bigger than $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 Yiisoft\Validator\SkipOnEmptyInterface. |
| $skipOnError | boolean |
Whether to skip this rule if any of the previous rules gave an error. See \Yiisoft\Validator\Rule\SkipOnErrorInterface. |
| $when | Closure|null |
A callable to define a condition for applying the rule. 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 getGreaterThanMaxMessage( ): string | ||
| return | string |
Error message. |
|---|---|---|
public function getGreaterThanMaxMessage(): string
{
return $this->greaterThanMaxMessage;
}
| public getHandler( ): string |
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 getIncorrectInputMessage( ): string | ||
| 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 getLessThanMinMessage( ): string | ||
| 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 getMax( ): float|integer|null | ||
| 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 getMin( ): float|integer|null | ||
| 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 getName( ): string |
public function getName(): string
{
return static::class;
}
Defined in: Yiisoft\Validator\Rule\AbstractNumber::getNotNumberMessage()
Get error message used when the value does not match $pattern
| public getNotNumberMessage( ): string | ||
| return | string |
Error message. |
|---|---|---|
public function getNotNumberMessage(): string
{
return $this->notNumberMessage;
}
| public getOptions( ): array |
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 getPattern( ): string | ||
| 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 getSkipOnEmpty( ): boolean|callable|null | ||
| 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 getWhen( ): Closure|null | ||
| 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 shouldSkipOnError( ): boolean | ||
| 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 skipOnEmpty( boolean|callable|null $value ): $this | ||
| $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 skipOnError( boolean $value ): $this | ||
| $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 when( Closure|null $value ): $this | ||
| $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.