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 __construct( callable|null $callback = null, string|null $method = null, boolean|callable|null $skipOnEmpty = null, boolean $skipOnError = false, Closure|null $when = null ): mixed | ||
| $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 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. |
| throws | InvalidArgumentException |
When neither $callback nor $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 afterInitAttribute( object $object ): void | ||
| $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 getCallback( ): callable|null | ||
| return | callable|null |
The callable that performs validation. |
|---|---|---|
public function getCallback(): ?callable
{
return $this->callback;
}
| public getHandler( ): string |
public function getHandler(): string
{
return CallbackHandler::class;
}
Get a name of a validated object method that performs the validation.
| public getMethod( ): string|null | ||
| return | string|null |
Name of a method that performs the validation. |
|---|---|---|
public function getMethod(): ?string
{
return $this->method;
}
| public getOptions( ): array |
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 getSkipOnEmpty( ): boolean|callable|null | ||
| 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 getWhen( ): Closure|null | ||
| 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 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.