0 follower

Final Class Yiisoft\Validator\Rule\BooleanValue

InheritanceYiisoft\Validator\Rule\BooleanValue
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

Contains a set of options to determine if the value is "true" or "false", not limited to boolean type only. What values exactly are considered "true" and "false" can be configured via BooleanValue::$trueValue and BooleanValue::$falseValue settings accordingly. There is also an option to choose between strict and non-strict mode of comparison (see BooleanValue::$strict).

See also Yiisoft\Validator\Rule\BooleanValueHandler Corresponding handler performing the actual validation.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Validator\Rule\BooleanValue
getFalseValue() Gets the value that is considered to be "false". Yiisoft\Validator\Rule\BooleanValue
getHandler() Yiisoft\Validator\Rule\BooleanValue
getIncorrectInputMessage() Gets error message used when validation fails because the type of validated value is incorrect. Yiisoft\Validator\Rule\BooleanValue
getMessage() Gets error message used when validation fails because the validated value does not match neither "true" nor "false" values. Yiisoft\Validator\Rule\BooleanValue
getName() Yiisoft\Validator\Rule\BooleanValue
getOptions() Yiisoft\Validator\Rule\BooleanValue
getSkipOnEmpty() A getter for $skipOnEmpty property. Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait
getTrueValue() Gets the value that is considered to be "true". Yiisoft\Validator\Rule\BooleanValue
getWhen() A getter for $when property. Yiisoft\Validator\Rule\Trait\WhenTrait
isStrict() Whether the comparison to $trueValue and $falseValue is strict. Yiisoft\Validator\Rule\BooleanValue
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

Method Details

Hide inherited methods

__construct() public method

public __construct( scalar $trueValue '1', scalar $falseValue '0', boolean $strict false, string $incorrectInputMessage 'The allowed types for {property} are integer, float, string, ' 'boolean. {type} given.', string $message '{Property} must be either "{true}" or "{false}".', boolean|callable|null $skipOnEmpty null, boolean $skipOnError false, Closure|null $when null ): mixed
$trueValue scalar

The value that is considered to be "true". Only scalar values (either int, float, string or bool) are allowed. Defaults to 1 string.

$falseValue scalar

The value that is considered to be "false". Only scalar values (either int, float, string or bool) are allowed. Defaults to 0 string.

$strict boolean

Whether the comparison to $trueValue and $falseValue is strict:

Defaults to false meaning non-strict mode is used.

$incorrectInputMessage string

Error message used when validation fails because the type of validated value is incorrect. Only scalar values are allowed - either int, float, string or bool. Used for more predictable formatting.

You may use the following placeholders in the message:

  • {property}: the translated label of the property being validated.
  • {true}: the value set in $trueValue option.
  • {false}: the value set in $falseValue option.
  • {type}: the type of the value being validated.
$message string

Error message used when validation fails because the validated value does not match neither "true" nor "false" values.

You may use the following placeholders in the message:

  • {property}: the translated label of the property being validated.
  • {true}: the value set in $trueValue option.
  • {false}: the value set in $falseValue option.
  • {value}: the value being validated.
$skipOnEmpty boolean|callable|null

Whether to skip this rule if the validated value is empty / not passed. See Yiisoft\Validator\SkipOnEmptyInterface.

$skipOnError boolean

Whether to skip this rule if any of the previous rules gave an error. See Yiisoft\Validator\SkipOnErrorInterface.

$when Closure|null

A callable to define a condition for applying the rule. See Yiisoft\Validator\WhenInterface.

                public function __construct(
    private int|float|string|bool $trueValue = '1',
    private int|float|string|bool $falseValue = '0',
    private bool $strict = false,
    private string $incorrectInputMessage = 'The allowed types for {property} are integer, float, string, '
    . 'boolean. {type} given.',
    private string $message = '{Property} must be either "{true}" or "{false}".',
    bool|callable|null $skipOnEmpty = null,
    private bool $skipOnError = false,
    private ?Closure $when = null,
) {
    $this->skipOnEmpty = $skipOnEmpty;
}

            
getFalseValue() public method

Gets the value that is considered to be "false".

public getFalseValue( ): scalar
return scalar

A scalar value.

                public function getFalseValue(): int|float|string|bool
{
    return $this->falseValue;
}

            
getHandler() public method

public getHandler( ): string

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

            
getIncorrectInputMessage() public method

Gets error message used when validation fails because the type of validated value is incorrect.

public getIncorrectInputMessage( ): string
return string

Error message / template.

                public function getIncorrectInputMessage(): string
{
    return $this->incorrectInputMessage;
}

            
getMessage() public method

Gets error message used when validation fails because the validated value does not match neither "true" nor "false" values.

public getMessage( ): string
return string

Error message / template.

                public function getMessage(): string
{
    return $this->message;
}

            
getName() public method

public getName( ): string

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

            
getOptions() public method

public getOptions( ): array

                public function getOptions(): array
{
    $messageParameters = [
        'true' => $this->trueValue === true ? 'true' : $this->trueValue,
        'false' => $this->falseValue === false ? 'false' : $this->falseValue,
    ];
    return [
        'trueValue' => $this->trueValue,
        'falseValue' => $this->falseValue,
        'strict' => $this->strict,
        'incorrectInputMessage' => [
            'template' => $this->incorrectInputMessage,
            'parameters' => $messageParameters,
        ],
        'message' => [
            'template' => $this->message,
            'parameters' => $messageParameters,
        ],
        'skipOnEmpty' => $this->getSkipOnEmptyOption(),
        'skipOnError' => $this->skipOnError,
    ];
}

            
getSkipOnEmpty() public method

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

            
getTrueValue() public method

Gets the value that is considered to be "true".

public getTrueValue( ): scalar
return scalar

A scalar value.

                public function getTrueValue(): int|float|string|bool
{
    return $this->trueValue;
}

            
getWhen() public method

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

A getter for $when property.

public getWhen( ): Closure|null
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;
}

            
isStrict() public method

Whether the comparison to $trueValue and $falseValue is strict.

public isStrict( ): boolean
return boolean

true - strict, false - non-strict.

                public function isStrict(): bool
{
    return $this->strict;
}

            
shouldSkipOnError() public method

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

A getter for $skipOnError property.

public shouldSkipOnError( ): boolean
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 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;
}

            
skipOnError() public method

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. 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 when( Closure|null $value ): $this
$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;
}