没有命名空间的类 yii yii\base yii\behaviors yii\caching yii\captcha yii\console yii\console\controllers yii\console\widgets yii\data yii\db yii\db\conditions yii\db\cubrid yii\db\cubrid\conditions yii\db\mssql yii\db\mssql\conditions yii\db\mysql yii\db\oci yii\db\oci\conditions yii\db\pgsql yii\db\sqlite yii\db\sqlite\conditions yii\di yii\filters yii\filters\auth yii\grid yii\helpers yii\i18n yii\log yii\mail yii\mutex yii\rbac yii\rest yii\test yii\validators yii\web yii\widgets

Class yii\validators\UniqueValidator

继承yii\validators\UniqueValidator » yii\validators\Validator » yii\base\Component » yii\base\BaseObject
实现yii\base\Configurable
可用版本自2.0
源码 https://github.com/yiichina/yii2/blob/api/framework/validators/UniqueValidator.php

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']]

公共属性

隐藏继承的属性

属性类型描述被定义在
$attributeNames array Attribute names. yii\validators\Validator
$attributes array|string Attributes to be validated by this validator. yii\validators\Validator
$behaviors yii\base\Behavior[] List of behaviors attached to this component yii\base\Component
$builtInValidators array List of built-in validators (name => class or configuration) yii\validators\Validator
$comboNotUnique string yii\validators\UniqueValidator
$enableClientValidation boolean Whether to enable client-side validation for this validator. yii\validators\Validator
$except array|string Scenarios that the validator should not be applied to. yii\validators\Validator
$filter string|array|Closure Additional filter to be applied to the DB query used to check the uniqueness of the attribute value. yii\validators\UniqueValidator
$forceMasterDb boolean Whether this validator is forced to always use master DB yii\validators\UniqueValidator
$isEmpty callable A PHP callable that replaces the default implementation of isEmpty(). yii\validators\Validator
$message string The user-defined error message. yii\validators\UniqueValidator
$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
$targetAttribute string|array The name of the ActiveRecord attribute that should be used to validate the uniqueness of the current attribute value. yii\validators\UniqueValidator
$targetAttributeJunction string And|or define how target attributes are related yii\validators\UniqueValidator
$targetClass string The name of the ActiveRecord class that should be used to validate the uniqueness of the current attribute value. yii\validators\UniqueValidator
$validationAttributes yii\validators\Validator
$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

公共方法

隐藏继承的方法

方法描述被定义在
__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() Returns the JavaScript needed for performing client-side validation. yii\validators\Validator
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() Returns the client-side validation options. yii\validators\Validator
getValidationAttributes() 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() Initializes the object. yii\validators\UniqueValidator
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() Validates a single attribute. yii\validators\UniqueValidator
validateAttributes() Validates the specified object. yii\validators\Validator

受保护的方法

隐藏继承的方法

方法描述被定义在
formatMessage() Formats a mesage using the I18N, or simple strtr if \Yii::$app is not available. yii\validators\Validator
validateValue() Validates a value. yii\validators\Validator

属性详情

$comboNotUnique 公共 属性 (自版本 2.0.9 可用)
Deprecated since version 2.0.10, to be removed in 2.1. Use $message property to setup custom message for multiple target attributes.
public string $comboNotUnique null
$filter 公共 属性

Additional 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 yii\db\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.

public string|array|Closure $filter null
$forceMasterDb 公共 属性 (自版本 2.0.14 可用)

Whether this validator is forced to always use master DB

public boolean $forceMasterDb true
$message 公共 属性

The 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.
public string $message null
$targetAttribute 公共 属性

The 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.

$targetAttributeJunction 公共 属性 (自版本 2.0.11 可用)

And|or define how target attributes are related

$targetClass 公共 属性

The 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.

参见 $targetAttribute.

public string $targetClass null

方法详情

init() 公共 方法

Initializes the object.

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

public void init()
validateAttribute() 公共 方法

Validates a single attribute.

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

public void validateAttribute($model, $attribute)
$model yii\base\Model

The data model to be validated

$attribute string

The name of the attribute to be validated.