Abstract Class Yiisoft\Validator\Rule\AbstractNumber
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\AbstractNumber::$pattern}. Optionally, you may configure the {@see \Yiisoft\Validator\Rule\AbstractNumber::$min} and {@see \Yiisoft\Validator\Rule\AbstractNumber::$max} to ensure the number is within a certain range.
See also Yiisoft\Validator\Rule\NumberHandler.
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, float|integer|null $max, string $incorrectInputMessage, string $notNumberMessage, string $lessThanMinMessage, string $greaterThanMaxMessage, string $pattern, boolean|callable|null $skipOnEmpty, boolean $skipOnError, Closure|null $when ) | ||
| $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. |
| $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\SkipOnErrorInterface}. |
| $when | Closure|null |
A callable to define a condition for applying the rule. See {@see \Yiisoft\Validator\WhenInterface}. |
public function __construct(
private float|int|null $min,
private float|int|null $max,
private string $incorrectInputMessage,
private string $notNumberMessage,
private string $lessThanMinMessage,
private string $greaterThanMaxMessage,
string $pattern,
bool|callable|null $skipOnEmpty,
private bool $skipOnError,
private ?Closure $when,
) {
if ($pattern === '') {
throw new InvalidArgumentException('Pattern can\'t be empty.');
}
$this->pattern = $pattern;
$this->skipOnEmpty = $skipOnEmpty;
}
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;
}
Get error message used when the value is not numeric.
| public string getIncorrectInputMessage ( ) | ||
| return | string |
Error message. |
|---|---|---|
public function getIncorrectInputMessage(): string
{
return $this->incorrectInputMessage;
}
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;
}
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;
}
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;
}
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,
];
}
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.