# Validator

> Validator is the base class for all validators.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [validators](/docs/api/yii/validators/index.md) › Validator

| class | `yii\validators\Validator` |
| --- | --- |
| Extends | [`yii\base\Component`](/docs/api/yii/base/component/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/validators/Validator.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/validators/Validator.php#L65) |

22 subclasses:

- [`yii\captcha\CaptchaValidator`](/docs/api/yii/captcha/captchavalidator/index.md)
- [`BooleanValidator`](/docs/api/yii/validators/booleanvalidator/index.md)
- [`CompareValidator`](/docs/api/yii/validators/comparevalidator/index.md)
- [`DateValidator`](/docs/api/yii/validators/datevalidator/index.md)
- [`DefaultValueValidator`](/docs/api/yii/validators/defaultvaluevalidator/index.md)
- [`EachValidator`](/docs/api/yii/validators/eachvalidator/index.md)
- [`EmailValidator`](/docs/api/yii/validators/emailvalidator/index.md)
- [`ExistValidator`](/docs/api/yii/validators/existvalidator/index.md)
- [`FileValidator`](/docs/api/yii/validators/filevalidator/index.md)
- [`FilterValidator`](/docs/api/yii/validators/filtervalidator/index.md)
- [`InlineValidator`](/docs/api/yii/validators/inlinevalidator/index.md)
- [`IpValidator`](/docs/api/yii/validators/ipvalidator/index.md)
- [`NumberValidator`](/docs/api/yii/validators/numbervalidator/index.md)
- [`RangeValidator`](/docs/api/yii/validators/rangevalidator/index.md)
- [`RegularExpressionValidator`](/docs/api/yii/validators/regularexpressionvalidator/index.md)
- [`RequiredValidator`](/docs/api/yii/validators/requiredvalidator/index.md)
- [`SafeValidator`](/docs/api/yii/validators/safevalidator/index.md)
- [`StringValidator`](/docs/api/yii/validators/stringvalidator/index.md)
- [`TrimValidator`](/docs/api/yii/validators/trimvalidator/index.md)
- [`UniqueValidator`](/docs/api/yii/validators/uniquevalidator/index.md)
- [`UrlValidator`](/docs/api/yii/validators/urlvalidator/index.md)
- [`yii2\extensions\filepond\validator\EncodedFileValidator`](/docs/extensions/filepond/reference/validator/encodedfilevalidator/index.md)

Validator is the base class for all validators.

Child classes should override the [`validateValue()`](/docs/api/yii/validators/validator/index.md#method-validateValue) and/or [`validateAttribute()`](/docs/api/yii/validators/validator/index.md#method-validateAttribute) methods to provide the actual
logic of performing data validation. Child classes may also override [`clientValidateAttribute()`](/docs/api/yii/validators/validator/index.md#method-clientValidateAttribute)
to provide client-side validation support.

Validator declares a set of [built-in validators](/docs/api/yii/validators/validator/index.md#property-builtInValidators) which can
be referenced using short names. They are listed as follows:

- `boolean`: [`BooleanValidator`](/docs/api/yii/validators/booleanvalidator/index.md)
- `captcha`: [`CaptchaValidator`](/docs/api/yii/captcha/captchavalidator/index.md)
- `compare`: [`CompareValidator`](/docs/api/yii/validators/comparevalidator/index.md)
- `date`: [`DateValidator`](/docs/api/yii/validators/datevalidator/index.md)
- `datetime`: [`DateValidator`](/docs/api/yii/validators/datevalidator/index.md)
- `time`: [`DateValidator`](/docs/api/yii/validators/datevalidator/index.md)
- `default`: [`DefaultValueValidator`](/docs/api/yii/validators/defaultvaluevalidator/index.md)
- `double`: [`NumberValidator`](/docs/api/yii/validators/numbervalidator/index.md)
- `each`: [`EachValidator`](/docs/api/yii/validators/eachvalidator/index.md)
- `email`: [`EmailValidator`](/docs/api/yii/validators/emailvalidator/index.md)
- `exist`: [`ExistValidator`](/docs/api/yii/validators/existvalidator/index.md)
- `file`: [`FileValidator`](/docs/api/yii/validators/filevalidator/index.md)
- `filter`: [`FilterValidator`](/docs/api/yii/validators/filtervalidator/index.md)
- `image`: [`ImageValidator`](/docs/api/yii/validators/imagevalidator/index.md)
- `in`: [`RangeValidator`](/docs/api/yii/validators/rangevalidator/index.md)
- `integer`: [`NumberValidator`](/docs/api/yii/validators/numbervalidator/index.md)
- `match`: [`RegularExpressionValidator`](/docs/api/yii/validators/regularexpressionvalidator/index.md)
- `required`: [`RequiredValidator`](/docs/api/yii/validators/requiredvalidator/index.md)
- `safe`: [`SafeValidator`](/docs/api/yii/validators/safevalidator/index.md)
- `string`: [`StringValidator`](/docs/api/yii/validators/stringvalidator/index.md)
- `trim`: [`TrimValidator`](/docs/api/yii/validators/trimvalidator/index.md)
- `unique`: [`UniqueValidator`](/docs/api/yii/validators/uniquevalidator/index.md)
- `url`: [`UrlValidator`](/docs/api/yii/validators/urlvalidator/index.md)
- `ip`: [`IpValidator`](/docs/api/yii/validators/ipvalidator/index.md)

For more details and usage information on Validator, see the [guide article on validators](/docs/guide/input-validation/index.md).

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-attributeNames"></span>`$attributeNames` read-only | `array` | Attribute names. | `Validator` |
| [`$attributes`](#property-attributes) | `array\|string` | Attributes to be validated by this validator. Default: `[]`. | `Validator` |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="property-builtInValidators"></span>`$builtInValidators` static | `array` | List of built-in validators (name => class or configuration) Default: `['boolean' => 'yii\validators\BooleanValidator', 'captcha' => 'yii\captcha\CaptchaValidator', 'compare' => 'yii\validators\CompareValidator', 'date' => 'yii\validators\DateValidator', 'datetime' => ['class' => 'yii\validators\DateValidator', 'type' => \yii\validators\DateValidator::TYPE_DATETIME], 'time' => ['class' => 'yii\validators\DateValidator', 'type' => \yii\validators\DateValidator::TYPE_TIME], 'default' => 'yii\validators\DefaultValueValidator', 'double' => 'yii\validators\NumberValidator', 'each' => 'yii\validators\EachValidator', 'email' => 'yii\validators\EmailValidator', 'exist' => 'yii\validators\ExistValidator', 'file' => 'yii\validators\FileValidator', 'filter' => 'yii\validators\FilterValidator', 'image' => 'yii\validators\ImageValidator', 'in' => 'yii\validators\RangeValidator', 'integer' => ['class' => 'yii\validators\NumberValidator', 'integerOnly' => true], 'match' => 'yii\validators\RegularExpressionValidator', 'number' => 'yii\validators\NumberValidator', 'required' => 'yii\validators\RequiredValidator', 'safe' => 'yii\validators\SafeValidator', 'string' => 'yii\validators\StringValidator', 'trim' => ['class' => 'yii\validators\TrimValidator', 'skipOnArray' => true], 'unique' => 'yii\validators\UniqueValidator', 'url' => 'yii\validators\UrlValidator', 'ip' => 'yii\validators\IpValidator']`. | `Validator` |
| [`$enableClientValidation`](#property-enableClientValidation) | `bool` | Whether to enable client-side validation for this validator. Default: `true`. | `Validator` |
| [`$except`](#property-except) | `array\|string` | Scenarios that the validator should not be applied to. Default: `[]`. | `Validator` |
| [`$isEmpty`](#property-isEmpty) | `callable\|null` | A PHP callable that replaces the default implementation of [`isEmpty()`](/docs/api/yii/validators/validator/index.md#method-isEmpty). Default: `null`. | `Validator` |
| [`$message`](#property-message) | `string\|null` | The user-defined error message. Default: `null`. | `Validator` |
| [`$on`](#property-on) | `array\|string` | Scenarios that the validator can be applied to. Default: `[]`. | `Validator` |
| [`$skipOnEmpty`](#property-skipOnEmpty) | `bool` | Whether this validation rule should be skipped if the attribute value is null or an empty string. Default: `true`. | `Validator` |
| [`$skipOnError`](#property-skipOnError) | `bool` | Whether this validation rule should be skipped if the attribute being validated already has some validation error according to some previous rules. Default: `true`. | `Validator` |
| <span id="property-validationAttributes"></span>`$validationAttributes` read-only | `array\|null` | List of attribute names. | `Validator` |
| [`$when`](#property-when) | `callable\|null` | A PHP callable whose return value determines whether this validator should be applied. Default: `null`. | `Validator` |
| [`$whenClient`](#property-whenClient) | `string\|null` | A JavaScript function name whose return value determines whether this validator should be applied on the client-side. Default: `null`. | `Validator` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/component/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__clone"></span>[`__clone()`](/docs/api/yii/base/component/index.md#method-__clone) | This method is called after the object is created by cloning an existing one. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/component/index.md#method-__get) | Returns the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/component/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/component/index.md#method-__set) | Sets the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/component/index.md#method-__unset) | Sets a component property to be null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`addError()`](#method-addError) | Adds an error about the specified attribute to the model object. | `Validator` |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehaviors"></span>[`attachBehaviors()`](/docs/api/yii/base/component/index.md#method-attachBehaviors) | Attaches a list of behaviors to the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/component/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/component/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`clientValidateAttribute()`](#method-clientValidateAttribute) | Returns the JavaScript needed for performing client-side validation. | `Validator` |
| [`createValidator()`](#method-createValidator) static | Creates a validator object. | `Validator` |
| <span id="method-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-detachBehaviors"></span>[`detachBehaviors()`](/docs/api/yii/base/component/index.md#method-detachBehaviors) | Detaches all behaviors from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-ensureBehaviors"></span>[`ensureBehaviors()`](/docs/api/yii/base/component/index.md#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`getAttributeNames()`](#method-getAttributeNames) | Returns cleaned attribute names without the `!` character at the beginning. | `Validator` |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehaviors"></span>[`getBehaviors()`](/docs/api/yii/base/component/index.md#method-getBehaviors) | Returns all behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`getClientOptions()`](#method-getClientOptions) | Returns the client-side validation options. | `Validator` |
| [`getFormattedClientMessage()`](#method-getFormattedClientMessage) | Returns the formatted validation message for client-side validation. | `Validator` |
| [`getValidationAttributes()`](#method-getValidationAttributes) | Returns a list of attributes this validator applies to. | `Validator` |
| <span id="method-hasEventHandlers"></span>[`hasEventHandlers()`](/docs/api/yii/base/component/index.md#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/component/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/component/index.md#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`init()`](#method-init) | Initializes the object. | `Validator` |
| [`isActive()`](#method-isActive) | Returns a value indicating whether the validator is active for the given scenario and attribute. | `Validator` |
| [`isEmpty()`](#method-isEmpty) | Checks if the given value is empty. | `Validator` |
| <span id="method-off"></span>[`off()`](/docs/api/yii/base/component/index.md#method-off) | Detaches an existing event handler from this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-on"></span>[`on()`](/docs/api/yii/base/component/index.md#method-on) | Attaches an event handler to an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`validate()`](#method-validate) | Validates a given value. | `Validator` |
| [`validateAttribute()`](#method-validateAttribute) | Validates a single attribute. | `Validator` |
| [`validateAttributes()`](#method-validateAttributes) | Validates the specified object. | `Validator` |
| [`formatMessage()`](#method-formatMessage) protected | Formats a mesage using the I18N, or simple strtr if `\Yii::$app` is not available. | `Validator` |
| [`validateValue()`](#method-validateValue) protected | Validates a value. | `Validator` |

## Property details

### $attributes <span id="property-attributes"></span>

```php
public array|string $attributes = []
```

Attributes to be validated by this validator.

For multiple attributes,
please specify them as an array; for single attribute, you may use either a string or an array.

### $enableClientValidation <span id="property-enableClientValidation"></span>

```php
public bool $enableClientValidation = true
```

Whether to enable client-side validation for this validator.

The actual client-side validation is done via the JavaScript code returned
by [`clientValidateAttribute()`](/docs/api/yii/validators/validator/index.md#method-clientValidateAttribute). If that method returns null, even if this property
is true, no client-side validation will be done by this validator.

### $except <span id="property-except"></span>

```php
public array|string $except = []
```

Scenarios that the validator should not be applied to.

For multiple scenarios,
please specify them as an array; for single scenario, you may use either a string or an array.

### $isEmpty <span id="property-isEmpty"></span>

```php
public callable|null $isEmpty = null
```

A PHP callable that replaces the default implementation of [`isEmpty()`](/docs/api/yii/validators/validator/index.md#method-isEmpty).

If not set, [`isEmpty()`](/docs/api/yii/validators/validator/index.md#method-isEmpty) will be used to check if a value is empty. The signature
of the callable should be `function ($value)` which returns a boolean indicating
whether the value is empty.

### $message <span id="property-message"></span>

```php
public string|null $message = null
```

The user-defined error message.

It may contain the following placeholders which
will be replaced accordingly by the validator:

- `{attribute}`: the label of the attribute being validated
- `{value}`: the value of the attribute being validated

Note that some validators may introduce other properties for error messages used when specific
validation conditions are not met. Please refer to individual class API documentation for details
about these properties. By convention, this property represents the primary error message
used when the most important validation condition is not met.

### $on <span id="property-on"></span>

```php
public array|string $on = []
```

Scenarios that the validator can be applied to.

For multiple scenarios,
please specify them as an array; for single scenario, you may use either a string or an array.

### $skipOnEmpty <span id="property-skipOnEmpty"></span>

```php
public bool $skipOnEmpty = true
```

Whether this validation rule should be skipped if the attribute value is null or an empty string.

This property is used only when validating [`Model`](/docs/api/yii/base/model/index.md).

### $skipOnError <span id="property-skipOnError"></span>

```php
public bool $skipOnError = true
```

Whether this validation rule should be skipped if the attribute being validated already has some validation error according to some previous rules.

Defaults to true.

### $when <span id="property-when"></span>

```php
public callable|null $when = null
```

A PHP callable whose return value determines whether this validator should be applied.

The signature of the callable should be `function ($model, $attribute)`, where `$model` and `$attribute`
refer to the model and the attribute currently being validated. The callable should return a boolean value.

This property is mainly provided to support conditional validation on the server-side.
If this property is not set, this validator will be always applied on the server-side.

The following example will enable the validator only when the country currently selected is USA:

```
function ($model) {
    return $model->country == Country::USA;
}
```

### $whenClient <span id="property-whenClient"></span>

```php
public string|null $whenClient = null
```

A JavaScript function name whose return value determines whether this validator should be applied on the client-side.

The signature of the function should be `function (attribute, value)`, where
`attribute` is an object describing the attribute being validated (see [`clientValidateAttribute()`](/docs/api/yii/validators/validator/index.md#method-clientValidateAttribute))
and `value` the current value of the attribute.

This property is mainly provided to support conditional validation on the client-side.
If this property is not set, this validator will be always applied on the client-side.

The following example will enable the validator only when the country currently selected is USA:

```
function (attribute, value) {
    return $('#country').val() === 'USA';
}
```

## Method details

### addError() <span id="method-addError"></span>

```php
public function addError(yii\base\Model $model, string $attribute, string $message, array $params = []): mixed
```

Adds an error about the specified attribute to the model object.

This is a helper method that performs message selection and internationalization.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$model` | [`yii\base\Model`](/docs/api/yii/base/model/index.md) | The data model being validated |
| `$attribute` | `string` | The attribute being validated |
| `$message` | `string` | The error message |
| `$params` | `array` | Values for the placeholders in the error message |

**Returns**

`mixed`

### clientValidateAttribute() <span id="method-clientValidateAttribute"></span>

```php
public function clientValidateAttribute(yii\base\Model $model, string $attribute, yii\web\View $view): string|null
```

Returns the JavaScript needed for performing client-side validation.

Calls [`getClientOptions()`](/docs/api/yii/validators/validator/index.md#method-getClientOptions) to generate options array for client-side validation.

You may override this method to return the JavaScript validation code if
the validator can support client-side validation.

The following JavaScript variables are predefined and can be used in the validation code:

- `attribute`: an object describing the the attribute being validated.
- `value`: the value being validated.
- `messages`: an array used to hold the validation error messages for the attribute.
- `deferred`: an array used to hold deferred objects for asynchronous validation
- `$form`: a jQuery object containing the form element

The `attribute` object contains the following properties:
- `id`: a unique ID identifying the attribute (e.g. "loginform-username") in the form
- `name`: attribute name or expression (e.g. "[0]content" for tabular input)
- `container`: the jQuery selector of the container of the input field
- `input`: the jQuery selector of the input field under the context of the form
- `error`: the jQuery selector of the error tag under the context of the container
- `status`: status of the input field, 0: empty, not entered before, 1: validated, 2: pending validation, 3: validating

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$model` | [`yii\base\Model`](/docs/api/yii/base/model/index.md) | The data model being validated |
| `$attribute` | `string` | The name of the attribute to be validated. |
| `$view` | [`yii\web\View`](/docs/api/yii/web/view/index.md) | The view object that is going to be used to render views or view files containing a model form with this validator applied. |

**Returns**

`string|null`: The client-side validation script. Null if the validator does not support client-side validation.

**See also**

- [`getClientOptions()`](/docs/api/yii/validators/validator/index.md#method-getClientOptions)
- [`ActiveForm::$enableClientValidation`](/docs/api/yii/widgets/activeform/index.md#property-enableClientValidation)

### createValidator() <span id="method-createValidator"></span>

```php
public static function createValidator(string|Closure $type, yii\base\Model $model, array|string $attributes, array $params = []): yii\validators\Validator
```

Creates a validator object.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$type` | `string\|`[`Closure`](https://www.php.net/manual/en/class.closure.php) | The validator type. This can be either: * a built-in validator name listed in [`$builtInValidators`](/docs/api/yii/validators/validator/index.md#property-builtInValidators); * a method name of the model class; * an anonymous function; * a validator class name. |
| `$model` | [`yii\base\Model`](/docs/api/yii/base/model/index.md) | The data model to be validated. |
| `$attributes` | `array\|string` | List of attributes to be validated. This can be either an array of the attribute names or a string of comma-separated attribute names. |
| `$params` | `array` | Initial values to be applied to the validator properties. |

**Returns**

[`Validator`](/docs/api/yii/validators/validator/index.md): The validator

### getAttributeNames() <span id="method-getAttributeNames"></span>

```php
public function getAttributeNames(): array
```

Returns cleaned attribute names without the `!` character at the beginning.

**Returns**

`array`: Attribute names.

**Since**

2.0.12

### getClientOptions() <span id="method-getClientOptions"></span>

```php
public function getClientOptions(yii\base\Model $model, string $attribute): array
```

Returns the client-side validation options.

This method is usually called from [`clientValidateAttribute()`](/docs/api/yii/validators/validator/index.md#method-clientValidateAttribute). You may override this method to modify options
that will be passed to the client-side validation.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$model` | [`yii\base\Model`](/docs/api/yii/base/model/index.md) | The model being validated |
| `$attribute` | `string` | The attribute name being validated |

**Returns**

`array`: The client-side validation options

**Since**

2.0.11

### getFormattedClientMessage() <span id="method-getFormattedClientMessage"></span>

```php
public function getFormattedClientMessage(string $message, array<string, mixed> $params): string
```

Returns the formatted validation message for client-side validation.

This method can be overridden in subclasses to provide custom message formatting with validator-specific
parameters.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$message` | `string` | The message template to be formatted |
| `$params` | `array<string, mixed>` | The parameters to be inserted into the message |

**Returns**

`string`: The formatted validation message

**Since**

22.0

### getValidationAttributes() <span id="method-getValidationAttributes"></span>

```php
public function getValidationAttributes(array|string|null $attributes = null): array|null
```

Returns a list of attributes this validator applies to.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$attributes` | `array\|string\|null` | The list of attributes to be validated. - If this is `null`, the result will be equal to [`getAttributeNames()`](/docs/api/yii/validators/validator/index.md#method-getAttributeNames). - If this is a string or an array, the intersection of [`getAttributeNames()`](/docs/api/yii/validators/validator/index.md#method-getAttributeNames) and the specified attributes will be returned. |

**Returns**

`array|null`: List of attribute names.

**Since**

2.0.16

### init() <span id="method-init"></span>

```php
public function init(): mixed
```

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the
given configuration.

**Returns**

`mixed`

### isActive() <span id="method-isActive"></span>

```php
public function isActive(string $scenario): bool
```

Returns a value indicating whether the validator is active for the given scenario and attribute.

A validator is active if

- the validator's `on` property is empty, or
- the validator's `on` property contains the specified scenario

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$scenario` | `string` | Scenario name |

**Returns**

`bool`: Whether the validator applies to the specified scenario.

### isEmpty() <span id="method-isEmpty"></span>

```php
public function isEmpty(mixed $value): bool
```

Checks if the given value is empty.

A value is considered empty if it is null, an empty array, or an empty string.
Note that this method is different from PHP empty(). It will return false when the value is 0.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `mixed` | The value to be checked |

**Returns**

`bool`: Whether the value is empty

### validate() <span id="method-validate"></span>

```php
public function validate(mixed $value, string|null &$error = null): bool
```

Validates a given value.

You may use this method to validate a value out of the context of a data model.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `mixed` | The data value to be validated. |
| `$error` | `string\|null` | The error message to be returned, if the validation fails. |

**Returns**

`bool`: Whether the data is valid.

### validateAttribute() <span id="method-validateAttribute"></span>

```php
public function validateAttribute(yii\base\Model $model, string $attribute): mixed
```

Validates a single attribute.

Child classes must implement this method to provide the actual validation logic.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$model` | [`yii\base\Model`](/docs/api/yii/base/model/index.md) | The data model to be validated |
| `$attribute` | `string` | The name of the attribute to be validated. |

**Returns**

`mixed`

### validateAttributes() <span id="method-validateAttributes"></span>

```php
public function validateAttributes(yii\base\Model $model, array|string|null $attributes = null): mixed
```

Validates the specified object.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$model` | [`yii\base\Model`](/docs/api/yii/base/model/index.md) | The data model being validated |
| `$attributes` | `array\|string\|null` | The list of attributes to be validated. Note that if an attribute is not associated with the validator - it will be ignored. If this parameter is null, every attribute listed in [`$attributes`](/docs/api/yii/validators/validator/index.md#property-attributes) will be validated. |

**Returns**

`mixed`

### formatMessage() <span id="method-formatMessage"></span>

```php
protected function formatMessage(string $message, array $params): string
```

Formats a mesage using the I18N, or simple strtr if `\Yii::$app` is not available.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$message` | `string` |  |
| `$params` | `array` |  |

**Returns**

`string`

**Since**

2.0.12

### validateValue() <span id="method-validateValue"></span>

```php
protected function validateValue(mixed $value): array|null
```

Validates a value.

A validator class can implement this method to support data validation out of the context of a data model.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `mixed` | The data value to be validated. |

**Returns**

`array|null`: The error message and the array of parameters to be inserted into the error message. if (!$valid) { return [$this->message, [ 'param1' => $this->param1, 'formattedLimit' => Yii::$app->formatter->asShortSize($this->getSizeLimit()), 'mimeTypes' => implode(', ', $this->mimeTypes), 'param4' => 'etc...', ]]; } return null; for this example `message` template can contain `{param1}`, `{formattedLimit}`, `{mimeTypes}`, `{param4}` Null should be returned if the data is valid.

**Throws**

- [`yii\base\NotSupportedException`](/docs/api/yii/base/notsupportedexception/index.md): if the validator does not supporting data validation without a model