Final Class Yiisoft\Validator\Rule\Callback
Defines validation options to validating the value using a callback.
See also Yiisoft\Validator\Rule\CallbackHandler.
Public Methods
Method Details
| public mixed __construct ( callable|null $callback = null, string|null $method = null, boolean|callable|null $skipOnEmpty = null, boolean $skipOnError = false, Closure|null $when = null ) | ||
| $callback | callable|null |
Callable with the |
| $method | string|null |
Name of a validated object method with the |
| $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}. |
| throws | InvalidArgumentException |
When neither {@see $callback} nor {@see $method} is specified or both are specified at the same time. |
|---|---|---|
public function __construct(
private mixed $callback = null,
private ?string $method = null,
bool|callable|null $skipOnEmpty = null,
private bool $skipOnError = false,
private ?Closure $when = null,
) {
if ($this->callback === null && $this->method === null) {
throw new InvalidArgumentException('Either "$callback" or "$method" must be specified.');
}
if ($this->callback !== null && $this->method !== null) {
throw new InvalidArgumentException('"$callback" and "$method" are mutually exclusive.');
}
$this->skipOnEmpty = $skipOnEmpty;
}
| public void afterInitAttribute ( object $object ) | ||
| $object | object | |
public function afterInitAttribute(object $object): void
{
if ($this->method === null) {
return;
}
$method = $this->method;
$reflection = new ReflectionObject($object);
if (!$reflection->hasMethod($method)) {
throw new InvalidArgumentException(
sprintf(
'Method "%s" does not exist in class "%s".',
$method,
$object::class,
),
);
}
/** @psalm-suppress MixedMethodCall */
$this->callback = Closure::bind(fn(mixed ...$args): mixed => $object->{$method}(...$args), $object, $object);
}
Get the callable that performs validation.
| public callable|null getCallback ( ) | ||
| return | callable|null |
The callable that performs validation. |
|---|---|---|
public function getCallback(): ?callable
{
return $this->callback;
}
| public string getHandler ( ) |
public function getHandler(): string
{
return CallbackHandler::class;
}
Get a name of a validated object method that performs the validation.
| public string|null getMethod ( ) | ||
| return | string|null |
Name of a method that performs the validation. |
|---|---|---|
public function getMethod(): ?string
{
return $this->method;
}
| public array getOptions ( ) |
public function getOptions(): array
{
return [
'method' => $this->method,
'skipOnEmpty' => $this->getSkipOnEmptyOption(),
'skipOnError' => $this->skipOnError,
];
}
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.