Final Class Yiisoft\Validator\Rule\TrueValue
A variation of Yiisoft\Validator\Rule\BooleanValue rule limiting the allowed values to "true" only (not limited to boolean "true" type). What value exactly is considered "true" can be configured via TrueValue::$trueValue setting. There is also an option to choose between strict and non-strict mode of comparison (see TrueValue::$strict).
A typical scope of application is a user agreement. If the purpose is to also check the falsiness, use Yiisoft\Validator\Rule\BooleanValue rule instead.
See also Yiisoft\Validator\Rule\TrueValueHandler Corresponding handler performing the actual validation.
Public Methods
Method Details
| public __construct( scalar $trueValue = '1', boolean $strict = false, string $incorrectInputMessage = 'The allowed types for {property} are integer, float, string, ' . 'boolean. {type} given.', string $message = '{Property} must be "{true}".', 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 |
| $strict | boolean |
Whether the comparison to $trueValue is strict:
Defaults to |
| $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:
|
| $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:
|
| $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 bool $strict = false,
private string $incorrectInputMessage = 'The allowed types for {property} are integer, float, string, '
. 'boolean. {type} given.',
private string $message = '{Property} must be "{true}".',
bool|callable|null $skipOnEmpty = null,
private bool $skipOnError = false,
private ?Closure $when = null,
) {
$this->skipOnEmpty = $skipOnEmpty;
}
| public getHandler( ): string |
public function getHandler(): string
{
return TrueValueHandler::class;
}
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;
}
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;
}
| public getOptions( ): array |
public function getOptions(): array
{
$messageParameters = [
'true' => $this->trueValue === true ? 'true' : $this->trueValue,
];
return [
'trueValue' => $this->trueValue,
'strict' => $this->strict,
'incorrectInputMessage' => [
'template' => $this->incorrectInputMessage,
'parameters' => $messageParameters,
],
'message' => [
'template' => $this->message,
'parameters' => $messageParameters,
],
'skipOnEmpty' => $this->getSkipOnEmptyOption(),
'skipOnError' => $this->skipOnError,
];
}
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;
}
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;
}
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;
}
Whether the comparison to $trueValue is strict.
| public isStrict( ): boolean | ||
| return | boolean |
|
|---|---|---|
public function isStrict(): bool
{
return $this->strict;
}
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.