0 follower

Final Class Yiisoft\Validator\Rule\Required

InheritanceYiisoft\Validator\Rule\Required
ImplementsYiisoft\Validator\DumpedRuleInterface, Yiisoft\Validator\SkipOnErrorInterface, Yiisoft\Validator\WhenInterface
Uses TraitsYiisoft\Validator\Rule\Trait\SkipOnErrorTrait, Yiisoft\Validator\Rule\Trait\WhenTrait

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

Hide inherited 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

Hide inherited methods

__construct() public method

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:

  • {property}: the translated label of the property being validated.
$notPassedMessage string

Error message used when validation fails because the validated value is not passed.

You may use the following placeholders in the message:

  • {property}: the translated label of the property being validated.
$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;
}

            
getEmptyCondition() public method

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

            
getHandler() public method

public string getHandler ( )

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

            
getMessage() public method

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

            
getName() public method

public string getName ( )

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

            
getNotPassedMessage() public method

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

            
getOptions() public method

public array getOptions ( )

                public function getOptions(): array
{
    return [
        'message' => [
            'template' => $this->message,
            'parameters' => [],
        ],
        'notPassedMessage' => [
            'template' => $this->notPassedMessage,
            'parameters' => [],
        ],
        'skipOnError' => $this->skipOnError,
    ];
}

            
getWhen() public method

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

A getter for $when property.

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

            
shouldSkipOnError() public method

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

A getter for $skipOnError property.

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

            
skipOnError() public method

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