Final Class Yiisoft\Validator\Rule\Required
Contains a set of options to determine if the value is not empty according to {@see Required::$emptyCondition}. When rule-level condition is not set, a handler-level condition ({@see RequiredHandler::$defaultEmptyCondition}) is applied (which is also customizable). In case of using attributes, the property must be present with passed non-empty value.
With default settings in order for value to pass the validation it must satisfy all the following conditions:
- Passed.
- Not
null. - Not an empty string (after trimming).
- Not an empty iterable.
When using with other rules, it must come first.
See also Yiisoft\Validator\Rule\RequiredHandler Corresponding handler performing the actual validation.
Psalm Types
| Name | Value |
|---|---|
| EmptyConditionType | callable |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Validator\Rule\Required | |
| getEmptyCondition() | Gets empty condition used to determine emptiness of the value. | Yiisoft\Validator\Rule\Required |
| getHandler() | Yiisoft\Validator\Rule\Required | |
| getMessage() | Gets error message used when validation fails because the validated value is empty. | Yiisoft\Validator\Rule\Required |
| getName() | Yiisoft\Validator\Rule\Required | |
| getNotPassedMessage() | Gets error message used when validation fails because the validated value is not passed. | Yiisoft\Validator\Rule\Required |
| getOptions() | Yiisoft\Validator\Rule\Required | |
| getWhen() | A getter for $when property. |
Yiisoft\Validator\Rule\Trait\WhenTrait |
| shouldSkipOnError() | A getter for $skipOnError property. |
Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait |
| 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
| public mixed __construct ( string $message = '{Property} cannot be blank.', string $notPassedMessage = '{Property} not passed.', callable|null $emptyCondition = null, boolean $skipOnError = false, Closure|null $when = null ) | ||
| $message | string |
Error message used when validation fails because the validated value is empty. You may use the following placeholders in the message:
|
| $notPassedMessage | string |
Error message used when validation fails because the validated value is not passed. You may use the following placeholders in the message:
|
| $emptyCondition | callable|null |
An empty condition used to determine emptiness of the value. |
| $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}. |
public function __construct(
private string $message = '{Property} cannot be blank.',
private string $notPassedMessage = '{Property} not passed.',
?callable $emptyCondition = null,
private bool $skipOnError = false,
private ?Closure $when = null,
) {
$this->emptyCondition = $emptyCondition;
}
Gets empty condition used to determine emptiness of the value.
| public callable|null getEmptyCondition ( ) | ||
| return | callable|null |
Empty condition. |
|---|---|---|
public function getEmptyCondition(): ?callable
{
return $this->emptyCondition;
}
| public string getHandler ( ) |
public function getHandler(): string
{
return RequiredHandler::class;
}
Gets error message used when validation fails because the validated value is empty.
| public string getMessage ( ) | ||
| return | string |
Error message / template. |
|---|---|---|
public function getMessage(): string
{
return $this->message;
}
Gets error message used when validation fails because the validated value is not passed.
| public string getNotPassedMessage ( ) | ||
| return | string |
Error message / template. |
|---|---|---|
public function getNotPassedMessage(): string
{
return $this->notPassedMessage;
}
| public array getOptions ( ) |
public function getOptions(): array
{
return [
'message' => [
'template' => $this->message,
'parameters' => [],
],
'notPassedMessage' => [
'template' => $this->notPassedMessage,
'parameters' => [],
],
'skipOnError' => $this->skipOnError,
];
}
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\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.