Class yii\validators\ExistValidator
Inheritance | yii\validators\ExistValidator » yii\validators\Validator » yii\base\Component » yii\base\BaseObject |
---|---|
Implements | yii\base\Configurable |
Available since version | 2.0 |
Source Code | https://github.com/yiisoft/yii2/blob/master/framework/validators/ExistValidator.php |
ExistValidator validates that the attribute value exists in a table.
ExistValidator checks if the value being validated can be found in the table column specified by the ActiveRecord class $targetClass and the attribute $targetAttribute. Since version 2.0.14 you can use more convenient attribute $targetRelation
This validator is often used to verify that a foreign key contains a value that can be found in the foreign table.
The following are examples of validation rules using this validator:
// a1 needs to exist
['a1', 'exist']
// a1 needs to exist, but its value will use a2 to check for the existence
['a1', 'exist', 'targetAttribute' => 'a2']
// a1 and a2 need to exist together, and they both will receive error message
[['a1', 'a2'], 'exist', 'targetAttribute' => ['a1', 'a2']]
// a1 and a2 need to exist together, only a1 will receive error message
['a1', 'exist', 'targetAttribute' => ['a1', 'a2']]
// a1 needs to exist by checking the existence of both a2 and a3 (using a1 value)
['a1', 'exist', 'targetAttribute' => ['a2', 'a1' => 'a3']]
// type_id needs to exist in the column "id" in the table defined in ProductType class
['type_id', 'exist', 'targetClass' => ProductType::class, 'targetAttribute' => ['type_id' => 'id']],
// the same as the previous, but using already defined relation "type"
['type_id', 'exist', 'targetRelation' => 'type'],
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$allowArray | boolean | Whether to allow array type attribute. | yii\validators\ExistValidator |
$attributeNames | array | Attribute names. This property is read-only. | 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. This property is read-only. | yii\base\Component |
$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 |
$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 existence of the attribute value. | yii\validators\ExistValidator |
$forceMasterDb | boolean | Whether this validator is forced to always use master DB | yii\validators\ExistValidator |
$isEmpty | callable | A PHP callable that replaces the default implementation of isEmpty(). | yii\validators\Validator |
$message | string | The user-defined error message. | yii\validators\Validator |
$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 existence of the current attribute value. | yii\validators\ExistValidator |
$targetAttributeJunction | string | And|or define how target attributes are related | yii\validators\ExistValidator |
$targetClass | string | The name of the ActiveRecord class that should be used to validate the existence of the current attribute value. | yii\validators\ExistValidator |
$targetRelation | string | The name of the relation that should be used to validate the existence of the current attribute value This param overwrites $targetClass and $targetAttribute | yii\validators\ExistValidator |
$validationAttributes | array | List of attribute names. This property is read-only. | 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 |
Public Methods
Method | Description | Defined 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() | 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() | 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() | Initializes the object. | yii\validators\ExistValidator |
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\ExistValidator |
validateAttributes() | Validates the specified object. | yii\validators\Validator |
Protected Methods
Method | Description | Defined By |
---|---|---|
createQuery() | Creates a query instance with the given condition. | yii\validators\ExistValidator |
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\ExistValidator |
Property Details
Whether to allow array type attribute.
Additional filter to be applied to the DB query used to check the existence 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.
Whether this validator is forced to always use master DB
The name of the ActiveRecord attribute that should be used to validate the existence 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 existence of multiple columns at the same time. The array key is the name of the attribute with the value to validate, the array value is the name of the database field to search.
And|or define how target attributes are related
The name of the ActiveRecord class that should be used to validate the existence of the current attribute value. If not set, it will use the ActiveRecord class of the attribute being validated.
See also $targetAttribute.
The name of the relation that should be used to validate the existence of the current attribute value This param overwrites $targetClass and $targetAttribute
Method Details
Creates a query instance with the given condition.
protected yii\db\ActiveQueryInterface createQuery ( $targetClass, $condition ) | ||
$targetClass | string | The target AR class |
$condition | mixed | Query condition |
return | yii\db\ActiveQueryInterface | The query instance |
---|
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 ( ) |
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. |
Validates a value.
A validator class can implement this method to support data validation out of the context of a data model.
protected array|null validateValue ( $value ) | ||
$value | mixed | The data value to be validated. |
return | array|null | The error message and the array of parameters to be inserted into the error message.
} return null;
Null should be returned if the data is valid. |
---|---|---|
throws | yii\base\NotSupportedException | if the validator does not supporting data validation without a model |