0 follower

Final Class Yiisoft\Validator\Rule\Number

InheritanceYiisoft\Validator\Rule\Number » Yiisoft\Validator\Rule\AbstractNumber
ImplementsYiisoft\Validator\DumpedRuleInterface, Yiisoft\Validator\SkipOnEmptyInterface, Yiisoft\Validator\SkipOnErrorInterface, Yiisoft\Validator\WhenInterface
Uses TraitsYiisoft\Validator\Rule\Trait\SkipOnEmptyTrait, Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait, Yiisoft\Validator\Rule\Trait\WhenTrait

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

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Validator\Rule\Number
getGreaterThanMaxMessage() Get error message used when the value is bigger than $max. Yiisoft\Validator\Rule\AbstractNumber
getHandler() Yiisoft\Validator\Rule\AbstractNumber
getIncorrectInputMessage() Get error message used when the value is not numeric. Yiisoft\Validator\Rule\AbstractNumber
getLessThanMinMessage() Get error message used when the value is smaller than $min. Yiisoft\Validator\Rule\AbstractNumber
getMax() Get upper limit of the number. null means no upper limit. Yiisoft\Validator\Rule\AbstractNumber
getMin() Get lower limit of the number. null means no lower limit. Yiisoft\Validator\Rule\AbstractNumber
getName() Yiisoft\Validator\Rule\AbstractNumber
getNotNumberMessage() Get error message used when the value does not match {@see $pattern} Yiisoft\Validator\Rule\AbstractNumber
getOptions() Yiisoft\Validator\Rule\AbstractNumber
getPattern() The regular expression for matching numbers. Yiisoft\Validator\Rule\AbstractNumber
getSkipOnEmpty() A getter for $skipOnEmpty property. Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait
getWhen() A getter for $when property. Yiisoft\Validator\Rule\Trait\WhenTrait
shouldSkipOnError() A getter for $skipOnError property. Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait
skipOnEmpty() An immutable setter to change $skipOnEmpty property. Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait
skipOnError() An immutable setter to change $skipOnError property. Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait
when() An immutable setter to change $when property. Yiisoft\Validator\Rule\Trait\WhenTrait

Constants

Hide inherited 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

Hide inherited methods

__construct() public method

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 null, meaning no lower limit. See {@see $lessThanMinMessage} for the customized message used when the number is too small.

$max float|integer|null

Upper limit of the number. Defaults to null, meaning no upper limit. See {@see $greaterThanMaxMessage} for the customized message used when the number is too big.

$incorrectInputMessage string

Error message used when the value is not numeric.

You may use the following placeholders in the message:

  • {property}: the translated label of the property being validated.
  • {type}: the type of the value being validated.
$notNumberMessage string

Error message used when the value does not match {@see $pattern}.

You may use the following placeholders in the message:

  • {property}: the translated label of the property being validated.
  • {value}: actual value.
$lessThanMinMessage string

Error message used when the value is smaller than {@link $min}.

You may use the following placeholders in the message:

  • {property}: the translated label of the property being validated.
  • {min}: minimum value.
  • {value}: actual value.
$greaterThanMaxMessage string

Error message used when the value is bigger than {@link $max}.

You may use the following placeholders in the message:

  • {property}: the translated label of the property being validated.
  • {max}: maximum value.
  • {value}: actual value.
$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,
    );
}

            
getGreaterThanMaxMessage() public method

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;
}

            
getHandler() public method
public string getHandler ( )

                public function getHandler(): string
{
    return NumberHandler::class;
}

            
getIncorrectInputMessage() public method

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;
}

            
getLessThanMinMessage() public method

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;
}

            
getMax() public method

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;
}

            
getMin() public method

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;
}

            
getName() public method
public string getName ( )

                public function getName(): string
{
    return static::class;
}

            
getNotNumberMessage() public method

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;
}

            
getOptions() public method
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,
    ];
}

            
getPattern() public method

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;
}

            
getSkipOnEmpty() public method

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;
}

            
getWhen() public method

Defined in: Yiisoft\Validator\Rule\Trait\WhenTrait::getWhen()

A getter for $when property.

public Closure|null getWhen ( )
return Closure|null

Current value:

  • null - always apply the validation.
  • callable - apply the validation depending on a return value: true - apply, false - do not apply.

                public function getWhen(): ?Closure
{
    return $this->when;
}

            
shouldSkipOnError() public method

Defined in: Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait::shouldSkipOnError()

A getter for $skipOnError property.

public boolean shouldSkipOnError ( )
return boolean

Current value. true means to skip the current rule when the previous one errored and false - do not skip.

                public function shouldSkipOnError(): bool
{
    return $this->skipOnError;
}

            
skipOnEmpty() public method

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;
}

            
skipOnError() public method

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. true means to skip the current rule when the previous one errored and false - do not skip.

return $this

The new instance with a changed value.

                public function skipOnError(bool $value): static
{
    $new = clone $this;
    $new->skipOnError = $value;
    return $new;
}

            
when() public method

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:

  • null - always apply the validation.
  • callable - apply the validation depending on a return value: true - apply, false - do not apply.
return $this

The new instance with a changed value.

                public function when(?Closure $value): static
{
    $new = clone $this;
    $new->when = $value;
    return $new;
}