0 follower

Final Class Yiisoft\Validator\Rule\Length

InheritanceYiisoft\Validator\Rule\Length
ImplementsYiisoft\Validator\CountableLimitInterface, Yiisoft\Validator\DumpedRuleInterface, Yiisoft\Validator\SkipOnEmptyInterface, Yiisoft\Validator\SkipOnErrorInterface, Yiisoft\Validator\WhenInterface
Uses TraitsYiisoft\Validator\Rule\Trait\CountableLimitTrait, Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait, Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait, Yiisoft\Validator\Rule\Trait\WhenTrait

Defines validation options to check that the value is a string of a certain length.

To check that the value only has the string type, use {@see \Yiisoft\Validator\Rule\StringValue} rule instead.

See also Yiisoft\Validator\Rule\LengthHandler.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Validator\Rule\Length
getEncoding() Get the encoding of the string value to be validated (e.g. 'UTF-8'). Yiisoft\Validator\Rule\Length
getExactly() A getter for {@see $exactly} property. Yiisoft\Validator\Rule\Trait\CountableLimitTrait
getGreaterThanMaxMessage() A getter for {@see $greaterThanMaxMessage} property. Yiisoft\Validator\Rule\Trait\CountableLimitTrait
getHandler() Yiisoft\Validator\Rule\Length
getIncorrectInputMessage() Get error message used when the value is neither an array nor implementing {@see \Countable} interface. Yiisoft\Validator\Rule\Length
getLessThanMinMessage() A getter for {@see $lessThanMinMessage} property. Yiisoft\Validator\Rule\Trait\CountableLimitTrait
getMax() A getter for {@see $max property}. Yiisoft\Validator\Rule\Trait\CountableLimitTrait
getMin() A getter for {@see $min} property. Yiisoft\Validator\Rule\Trait\CountableLimitTrait
getName() Yiisoft\Validator\Rule\Length
getNotExactlyMessage() A getter for {@see $notExactlyMessage} property. Yiisoft\Validator\Rule\Trait\CountableLimitTrait
getOptions() Yiisoft\Validator\Rule\Length
getSkipOnEmpty() A getter for $skipOnEmpty property. Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait
getWhen() A getter for $when property. Yiisoft\Validator\Rule\Trait\WhenTrait
shouldSkipOnError() A getter for $skipOnError property. Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait
skipOnEmpty() An immutable setter to change $skipOnEmpty property. Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait
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 ( integer|null $exactly null, integer|null $min null, integer|null $max null, string $incorrectInputMessage '{Property} must be a string. {type} given.', string $lessThanMinMessage '{Property} must contain at least {min, number} {min, plural, one{character} ' 'other{characters}}.', string $greaterThanMaxMessage '{Property} must contain at most {max, number} {max, plural, one{character} ' 'other{characters}}.', string $notExactlyMessage '{Property} must contain exactly {exactly, number} {exactly, plural, ' 'one{character} other{characters}}.', string $encoding 'UTF-8', boolean|callable|null $skipOnEmpty null, boolean $skipOnError false, Closure|null $when null )
$exactly integer|null

Exact length. null means no strict comparison. Mutually exclusive with {@see $min} and {@see $max}.

$min integer|null

Minimum length. null means no minimum length limit. Can't be combined with {@see $exactly}. See {@see $lessThanMinMessage} for the customized message for a too short string.

$max integer|null

Maximum length. null means no maximum length limit. Can't be combined with {@see $exactly}. See {@see $greaterThanMaxMessage} for the customized message for a too long string.

$incorrectInputMessage string

Error message used when the value is not a string.

You may use the following placeholders in the message:

  • {property}: the translated label of the property being validated.
  • {type}: the type of the value being validated.
$lessThanMinMessage string

Error message used when the length of the value is smaller than {@see $min}.

You may use the following placeholders in the message:

  • {property}: the translated label of the property being validated.
  • {min}: minimum number of items required.
  • {number}: actual number of items.
$greaterThanMaxMessage string

Error message used when the length of the value is greater than {@see $max}.

You may use the following placeholders in the message:

  • {property}: the translated label of the property being validated.
  • {max}: maximum number of items required.
  • {number}: actual number of items.
$notExactlyMessage string

Error message used when the number of items does not equal {@see $exactly}.

You may use the following placeholders in the message:

  • {property}: the translated label of the property being validated.
  • {exactly}: exact number of items required.
  • {number}: actual number of items.
$encoding string

The encoding of the string value to be validated (e.g. 'UTF-8'). If this property is not set, application wide encoding will be used.

$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}.

                public function __construct(
    ?int $exactly = null,
    ?int $min = null,
    ?int $max = null,
    private string $incorrectInputMessage = '{Property} must be a string. {type} given.',
    string $lessThanMinMessage = '{Property} must contain at least {min, number} {min, plural, one{character} '
    . 'other{characters}}.',
    string $greaterThanMaxMessage = '{Property} must contain at most {max, number} {max, plural, one{character} '
    . 'other{characters}}.',
    string $notExactlyMessage = '{Property} must contain exactly {exactly, number} {exactly, plural, '
    . 'one{character} other{characters}}.',
    private string $encoding = 'UTF-8',
    bool|callable|null $skipOnEmpty = null,
    private bool $skipOnError = false,
    private ?Closure $when = null,
) {
    $this->initCountableLimitProperties(
        $min,
        $max,
        $exactly,
        $lessThanMinMessage,
        $greaterThanMaxMessage,
        $notExactlyMessage,
    );
    $this->skipOnEmpty = $skipOnEmpty;
}

            
getEncoding() public method

Get the encoding of the string value to be validated (e.g. 'UTF-8').

If this property is not set, application wide encoding will be used.

public string getEncoding ( )
return string

Encoding of the string value to be validated.

                public function getEncoding(): string
{
    return $this->encoding;
}

            
getExactly() public method

Defined in: Yiisoft\Validator\Rule\Trait\CountableLimitTrait::getExactly()

A getter for {@see $exactly} property.

public integer|null getExactly ( )
return integer|null

A number representing "exactly" value. null means no strict comparison so lower / upper limits / both must be set.

                public function getExactly(): ?int
{
    return $this->exactly;
}

            
getGreaterThanMaxMessage() public method

Defined in: Yiisoft\Validator\Rule\Trait\CountableLimitTrait::getGreaterThanMaxMessage()

A getter for {@see $greaterThanMaxMessage} property.

public string getGreaterThanMaxMessage ( )
return string

Validation error message.

                public function getGreaterThanMaxMessage(): string
{
    return $this->greaterThanMaxMessage;
}

            
getHandler() public method

public string getHandler ( )

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

            
getIncorrectInputMessage() public method

Get error message used when the value is neither an array nor implementing {@see \Countable} interface.

public string getIncorrectInputMessage ( )
return string

Error message.

                public function getIncorrectInputMessage(): string
{
    return $this->incorrectInputMessage;
}

            
getLessThanMinMessage() public method

Defined in: Yiisoft\Validator\Rule\Trait\CountableLimitTrait::getLessThanMinMessage()

A getter for {@see $lessThanMinMessage} property.

public string getLessThanMinMessage ( )
return string

Validation error message.

                public function getLessThanMinMessage(): string
{
    return $this->lessThanMinMessage;
}

            
getMax() public method

Defined in: Yiisoft\Validator\Rule\Trait\CountableLimitTrait::getMax()

A getter for {@see $max property}.

public integer|null getMax ( )
return integer|null

A number representing maximum boundary. null means no upper bound.

                public function getMax(): ?int
{
    return $this->max;
}

            
getMin() public method

Defined in: Yiisoft\Validator\Rule\Trait\CountableLimitTrait::getMin()

A getter for {@see $min} property.

public integer|null getMin ( )
return integer|null

A number representing minimum boundary. null means no lower bound.

                public function getMin(): ?int
{
    return $this->min;
}

            
getName() public method

public string getName ( )

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

            
getNotExactlyMessage() public method

Defined in: Yiisoft\Validator\Rule\Trait\CountableLimitTrait::getNotExactlyMessage()

A getter for {@see $notExactlyMessage} property.

public string getNotExactlyMessage ( )
return string

Validation error message.

                public function getNotExactlyMessage(): string
{
    return $this->notExactlyMessage;
}

            
getOptions() public method

public array getOptions ( )

                public function getOptions(): array
{
    return array_merge($this->getLimitOptions(), [
        'incorrectInputMessage' => [
            'template' => $this->incorrectInputMessage,
            'parameters' => [],
        ],
        'encoding' => $this->encoding,
        'skipOnEmpty' => $this->getSkipOnEmptyOption(),
        'skipOnError' => $this->skipOnError,
    ]);
}

            
getSkipOnEmpty() public method

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

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

            
skipOnEmpty() public method

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

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