UniqueValidator
API reference › yii › validators › UniqueValidator
UniqueValidator validates that the attribute value is unique in the specified database table.
UniqueValidator checks if the value being validated is unique in the table column specified by
the ActiveRecord class $targetClass and the attribute $targetAttribute.
The following are examples of validation rules using this validator:
// a1 needs to be unique['a1', 'unique']// a1 needs to be unique, but column a2 will be used to check the uniqueness of the a1 value['a1', 'unique', 'targetAttribute' => 'a2']// a1 and a2 need to be unique together, and they both will receive error message[['a1', 'a2'], 'unique', 'targetAttribute' => ['a1', 'a2']]// a1 and a2 need to be unique together, only a1 will receive error message['a1', 'unique', 'targetAttribute' => ['a1', 'a2']]// a1 needs to be unique by checking the uniqueness of both a2 and a3 (using a1 value)['a1', 'unique', 'targetAttribute' => ['a2', 'a1' => 'a3']]Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$attributeNames read-only | array | Attribute names. | Validator |
$attributes | array| | Attributes to be validated by this validator. Default: []. | Validator |
$behaviors read-only | array<yii\<static>> | List of behaviors attached to this component. | yii\ |
$builtInValidators static | array | List of built-in validators (name => class or configuration) Default: ['boolean' => 'yii\. | Validator |
$enableClientValidation | bool | Whether to enable client-side validation for this validator. Default: true. | Validator |
$except | array| | Scenarios that the validator should not be applied to. Default: []. | Validator |
$filter | string|Closure | Additional filter to be applied to the DB query used to check the uniqueness of the attribute value. Default: null. | UniqueValidator |
$forceMasterDb | bool | Whether this validator is forced to always use master DB Default: true. Available since 2.0.14. | UniqueValidator |
$isEmpty | callable| | A PHP callable that replaces the default implementation of isEmpty(). Default: null. | Validator |
$message | string | The user-defined error message. Default: null. | UniqueValidator |
$on | array| | Scenarios that the validator can be applied to. Default: []. | Validator |
$skipOnEmpty | bool | Whether this validation rule should be skipped if the attribute value is null or an empty string. Default: true. | Validator |
$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 |
$targetAttribute | string| | The name of the ActiveRecord attribute that should be used to validate the uniqueness of the current attribute value. Default: null. | UniqueValidator |
$targetAttributeJunction | string | And|or define how target attributes are related Default: 'and'. Available since 2.0.11. | UniqueValidator |
$targetClass | string| | The name of the ActiveRecord class that should be used to validate the uniqueness of the current attribute value. Default: null. | UniqueValidator |
$validationAttributes read-only | array| | List of attribute names. | Validator |
$when | callable| | A PHP callable whose return value determines whether this validator should be applied. Default: null. | Validator |
$whenClient | string| | A JavaScript function name whose return value determines whether this validator should be applied on the client-side. Default: null. | Validator |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__clone() | This method is called after the object is created by cloning an existing one. | yii\ |
__get() | Returns the value of a component property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets the value of a component property. | yii\ |
__unset() | Sets a component property to be null. | yii\ |
addError() | Adds an error about the specified attribute to the model object. | Validator |
attachBehavior() | Attaches a behavior to this component. | yii\ |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\ |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
clientValidateAttribute() | Returns the JavaScript needed for performing client-side validation. | Validator |
createValidator() static | Creates a validator object. | Validator |
detachBehavior() | Detaches a behavior from the component. | yii\ |
detachBehaviors() | Detaches all behaviors from the component. | yii\ |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\ |
getAttributeNames() | Returns cleaned attribute names without the ! character at the beginning. | Validator |
getBehavior() | Returns the named behavior object. | yii\ |
getBehaviors() | Returns all behaviors attached to this component. | yii\ |
getClientOptions() | Returns the client-side validation options. | Validator |
getFormattedClientMessage() | Returns the formatted validation message for client-side validation. | Validator |
getValidationAttributes() | Returns a list of attributes this validator applies to. | Validator |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\ |
init() | Initializes the object. | UniqueValidator |
isActive() | Returns a value indicating whether the validator is active for the given scenario and attribute. | Validator |
isEmpty() | Checks if the given value is empty. | Validator |
off() | Detaches an existing event handler from this component. | yii\ |
on() | Attaches an event handler to an event. | yii\ |
trigger() | Triggers an event. | yii\ |
validate() | Validates a given value. | Validator |
validateAttribute() | Validates a single attribute. | UniqueValidator |
validateAttributes() | Validates the specified object. | Validator |
formatMessage() protected | Formats a mesage using the I18N, or simple strtr if \ is not available. | Validator |
validateValue() protected | Validates a value. | Validator |
Property details
Section titled “Property details”$filter
Section titled “$filter ”public string|array|Closure $filter = nullAdditional filter to be applied to the DB query used to check the uniqueness of the attribute value.
This can be a string or an array representing the additional query condition (refer to Query::where()
on the format of query condition), or an anonymous function with the signature function ($query), where $query
is the Query object that you can modify in the function.
$message
Section titled “$message ”public string $message = nullThe user-defined error message.
When validating single attribute, 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
When validating mutliple attributes, it may contain the following placeholders:
{attributes}: the labels of the attributes being validated.{values}: the values of the attributes being validated.
$targetAttribute
Section titled “$targetAttribute ”public string|array|null $targetAttribute = nullThe name of the ActiveRecord attribute that should be used to validate the uniqueness of the current attribute value.
If not set, it will use the name of the attribute currently being validated. You may use an array to validate the uniqueness of multiple columns at the same time. The array values are the attributes that will be used to validate the uniqueness, while the array keys are the attributes whose values are to be validated.
$targetClass
Section titled “$targetClass ”public string|null $targetClass = nullThe name of the ActiveRecord class that should be used to validate the uniqueness of the current attribute value.
If not set, it will use the ActiveRecord class of the attribute being validated.
Method details
Section titled “Method details”init()
Section titled “init() ”public function init(): mixedInitializes the object.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
Returns
mixed
validateAttribute()
Section titled “validateAttribute() ”public function validateAttribute(mixed $model, mixed $attribute): mixedValidates a single attribute.
Child classes must implement this method to provide the actual validation logic.
Parameters
| Parameter | Type | Description |
|---|---|---|
$model | mixed | The data model to be validated |
$attribute | mixed | The name of the attribute to be validated. |
Returns
mixed