UniqueValidator ​
- Type
- Class
- Namespace
- craft\validators
- Inherits
- craft\validators\UniqueValidator » yii\validators\UniqueValidator » yii\validators\Validator » yii\base\Component » yii\base\BaseObject
- Implements
- yii\base\Configurable
- Since
- 3.0.0
Class UniqueValidator.
Public Properties ​
Property | Description |
---|---|
attributeNames | array – Attribute names. |
attributes | array, string – Attributes to be validated by this validator. |
behaviors | yii\base\Behavior – List of behaviors attached to this component. |
builtInValidators | array – List of built-in validators (name => class or configuration) |
caseInsensitive | boolean – Whether a case-insensitive check should be performed. |
comboNotUnique | string |
enableClientValidation | boolean – Whether to enable client-side validation for this validator. |
except | array, string – Scenarios that the validator should not be applied to. |
filter | string, array, Closure – Additional filter to be applied to the DB query used to check the uniqueness of the attribute value. |
forceMasterDb | boolean – Whether this validator is forced to always use master DB |
isEmpty | callable, null – A PHP callable that replaces the default implementation of isEmpty(). |
message | string – The user-defined error message. |
on | array, string – Scenarios that the validator can be applied to. |
pk | string, string[] – If targetClass is set, this defines the model attributes that represent the record's primary key(s). |
skipOnEmpty | boolean – Whether this validation rule should be skipped if the attribute value is null or an empty string. |
skipOnError | boolean – Whether this validation rule should be skipped if the attribute being validated already has some validation error according to some previous rules. |
targetAttribute | string, array, null – The name of the ActiveRecord attribute that should be used to validate the uniqueness of the current attribute value. |
targetAttributeJunction | string – And |
targetClass | string, null – The name of the ActiveRecord class that should be used to validate the uniqueness of the current attribute value. |
validationAttributes | array, null – List of attribute names. |
when | callable, null – A PHP callable whose return value determines whether this validator should be applied. |
whenClient | string, null – A JavaScript function name whose return value determines whether this validator should be applied on the client-side. |
caseInsensitive
​
- Type
- boolean
- Default value
false
Whether a case-insensitive check should be performed.
pk
​
If targetClass is set, this defines the model attributes that represent the record's primary key(s). Can be set to a string or array of strings of model attributes in the same respective order as the primary keys defined by the record's primaryKey() method, or can be set to an array of attribute/PK pairs, which explicitly maps model attributes to record primary keys. Defaults to whatever the record's primaryKey() method returns.
Protected Properties ​
Property | Description |
---|---|
originalModel | yii\base\Model, null – The model that is being validated |
originalModel
​
- Type
- yii\base\Model, null
- Default value
null
The model that is being validated
Public Methods ​
Method | Description |
---|---|
__call() | Calls the named method which is not a class method. |
__clone() | This method is called after the object is created by cloning an existing one. |
__construct() | Constructor. |
__get() | Returns the value of a component property. |
__isset() | Checks if a property is set, i.e. defined and not null. |
__set() | Sets the value of a component property. |
__unset() | Sets a component property to be null. |
addError() | Adds an error about the specified attribute to the model object. |
attachBehavior() | Attaches a behavior to this component. |
attachBehaviors() | Attaches a list of behaviors to the component. |
behaviors() | Returns a list of behaviors that this component should behave as. |
canGetProperty() | Returns a value indicating whether a property can be read. |
canSetProperty() | Returns a value indicating whether a property can be set. |
className() | Returns the fully qualified name of this class. |
clientValidateAttribute() | Returns the JavaScript needed for performing client-side validation. |
createValidator() | Creates a validator object. |
detachBehavior() | Detaches a behavior from the component. |
detachBehaviors() | Detaches all behaviors from the component. |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. |
getAttributeNames() | Returns cleaned attribute names without the ! character at the beginning. |
getBehavior() | Returns the named behavior object. |
getBehaviors() | Returns all behaviors attached to this component. |
getClientOptions() | Returns the client-side validation options. |
getValidationAttributes() | Returns a list of attributes this validator applies to. |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. |
hasMethod() | Returns a value indicating whether a method is defined. |
hasProperty() | Returns a value indicating whether a property is defined for this component. |
init() | Initializes the object. |
isActive() | Returns a value indicating whether the validator is active for the given scenario and attribute. |
isEmpty() | Checks if the given value is empty. |
off() | Detaches an existing event handler from this component. |
on() | Attaches an event handler to an event. |
trigger() | Triggers an event. |
validate() | Validates a given value. |
validateAttribute() | Validates a single attribute. |
validateAttributes() | Validates the specified object. |
addError()
​
Adds an error about the specified attribute to the model object.
This is a helper method that performs message selection and internationalization.
Arguments ​
$model
(yii\base\Model) – 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
validateAttribute()
​
Validates a single attribute.
Child classes must implement this method to provide the actual validation logic.
Arguments ​
$model
(yii\base\Model) – The data model to be validated$attribute
(string) – The name of the attribute to be validated.
Protected Methods ​
Method | Description |
---|---|
formatMessage() | Formats a mesage using the I18N, or simple strtr if \Yii::$app is not available. |
validateValue() | Validates a value. |