Class yii\validators\StringValidator

Inheritanceyii\validators\StringValidator » yii\validators\Validator » yii\base\Component » yii\base\BaseObject
Implementsyii\base\Configurable
Available since version2.0
Source Code https://github.com/yiisoft/yii2/blob/master/framework/validators/StringValidator.php

StringValidator validates that the attribute value is of certain length.

Note, this validator should only be used with string-typed attributes.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$attributes array|string Attributes to be validated by this validator. yii\validators\Validator
$builtInValidators array List of built-in validators (name => class or configuration) yii\validators\Validator
$enableClientValidation boolean Whether to enable client-side validation for this validator. yii\validators\Validator
$encoding string The encoding of the string value to be validated (e.g. 'UTF-8'). yii\validators\StringValidator
$except array|string Scenarios that the validator should not be applied to. yii\validators\Validator
$isEmpty callable A PHP callable that replaces the default implementation of isEmpty(). yii\validators\Validator
$length integer|array Specifies the length limit of the value to be validated. yii\validators\StringValidator
$max integer Maximum length. yii\validators\StringValidator
$message string User-defined error message used when the value is not a string. yii\validators\StringValidator
$min integer Minimum length. yii\validators\StringValidator
$notEqual string User-defined error message used when the length of the value is not equal to $length. yii\validators\StringValidator
$on array|string Scenarios that the validator can be applied to. yii\validators\Validator
$skipOnEmpty boolean Whether this validation rule should be skipped if the attribute value is null or an empty string. yii\validators\Validator
$skipOnError boolean Whether this validation rule should be skipped if the attribute being validated already has some validation error according to some previous rules. yii\validators\Validator
$strict boolean Whether to require the value to be a string data type. yii\validators\StringValidator
$tooLong string User-defined error message used when the length of the value is greater than $max. yii\validators\StringValidator
$tooShort string User-defined error message used when the length of the value is smaller than $min. yii\validators\StringValidator
$when callable A PHP callable whose return value determines whether this validator should be applied. yii\validators\Validator
$whenClient string A JavaScript function name whose return value determines whether this validator should be applied on the client-side. yii\validators\Validator

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. yii\base\Component
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of a component property. yii\base\Component
__isset() Checks if a property is set, i.e. defined and not null. yii\base\Component
__set() Sets the value of a component property. yii\base\Component
__unset() Sets a component property to be null. yii\base\Component
addError() Adds an error about the specified attribute to the model object. yii\validators\Validator
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
behaviors() Returns a list of behaviors that this component should behave as. yii\base\Component
canGetProperty() Returns a value indicating whether a property can be read. yii\base\Component
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Component
className() Returns the fully qualified name of this class. yii\base\BaseObject
clientValidateAttribute() {@inheritdoc} yii\validators\StringValidator
createValidator() Creates a validator object. yii\validators\Validator
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
getAttributeNames() Returns cleaned attribute names without the ! character at the beginning. yii\validators\Validator
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getClientOptions() {@inheritdoc} yii\validators\StringValidator
getValidationAttributes() Returns a list of attributes this validator applies to. yii\validators\Validator
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event. yii\base\Component
hasMethod() Returns a value indicating whether a method is defined. yii\base\Component
hasProperty() Returns a value indicating whether a property is defined for this component. yii\base\Component
init() {@inheritdoc} yii\validators\StringValidator
isActive() Returns a value indicating whether the validator is active for the given scenario and attribute. yii\validators\Validator
isEmpty() Checks if the given value is empty. yii\validators\Validator
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. yii\base\Component
trigger() Triggers an event. yii\base\Component
validate() Validates a given value. yii\validators\Validator
validateAttribute() {@inheritdoc} yii\validators\StringValidator
validateAttributes() Validates the specified object. yii\validators\Validator

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
formatMessage() Formats a mesage using the I18N, or simple strtr if \Yii::$app is not available. yii\validators\Validator
validateValue() {@inheritdoc} yii\validators\StringValidator

Property Details

$encoding public property

The encoding of the string value to be validated (e.g. 'UTF-8'). If this property is not set, yii\base\Application::$charset will be used.

public string $encoding null
$length public property

Specifies the length limit of the value to be validated. This can be specified in one of the following forms:

  • an integer: the exact length that the value should be of;
  • an array of one element: the minimum length that the value should be of. For example, [8]. This will overwrite $min.
  • an array of two elements: the minimum and maximum lengths that the value should be of. For example, [8, 128]. This will overwrite both $min and $max.

See also:

  • $tooShort for the customized message for a too short string.
  • $tooLong for the customized message for a too long string.
  • $notEqual for the customized message for a string that does not match desired length.
public integer|array $length null
$max public property

Maximum length. If not set, it means no maximum length limit.

See also $tooLong for the customized message for a too long string.

public integer $max null
$message public property

User-defined error message used when the value is not a string.

public string $message null
$min public property

Minimum length. If not set, it means no minimum length limit.

See also $tooShort for the customized message for a too short string.

public integer $min null
$notEqual public property

User-defined error message used when the length of the value is not equal to $length.

public string $notEqual null
$strict public property (available since version 2.0.33)

Whether to require the value to be a string data type. If false any scalar value will be treated as it's string equivalent.

public boolean $strict true
$tooLong public property

User-defined error message used when the length of the value is greater than $max.

public string $tooLong null
$tooShort public property

User-defined error message used when the length of the value is smaller than $min.

public string $tooShort null

Method Details

clientValidateAttribute() public method

{@inheritdoc}

public void clientValidateAttribute ( $model, $attribute, $view )
$model
$attribute
$view
getClientOptions() public method

{@inheritdoc}

public void getClientOptions ( $model, $attribute )
$model
$attribute
init() public method

{@inheritdoc}

public void init ( )
validateAttribute() public method

{@inheritdoc}

public void validateAttribute ( $model, $attribute )
$model
$attribute
validateValue() protected method

{@inheritdoc}

protected void validateValue ( $value )
$value