ExistValidator checks if the value being validated can be found in the table column specified by the ActiveRecord class [[targetClass]] and the attribute [[targetAttribute]]. 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: php 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']]
Since: 2.0
Author: Qiang Xue (qiang.xue@gmail.com)
Inheritance: extends Validator
 /**
  * @param \yii\base\Model $model
  * @param string $attribute
  */
 public function validateAttribute($model, $attribute)
 {
     $regexpValidator = new RegularExpressionValidator(['pattern' => '/^9[0-9]{9}$/']);
     if (!$regexpValidator->validate($model->{$attribute})) {
         $this->addError($model, $attribute, '{attribute} имеет неверный формат');
         return;
     }
     $existValidator = new ExistValidator(['targetClass' => Number::className(), 'targetAttribute' => 'number']);
     if (!$existValidator->validate($model->{$attribute})) {
         $this->addError($model, $attribute, "{attribute} не существует");
     }
 }
 public function validateKeyCodePair($attribute)
 {
     $existValidator = \Yii::createObject(['class' => ExistValidator::className(), 'targetClass' => Language::className(), 'targetAttribute' => 'code']);
     $errorLangExists = null;
     $existValidator->validate($this->lang_code, $errorLangExists);
     if (!empty($errorLangExists)) {
         $this->addError('lang_code', $errorLangExists);
     } else {
         if (($testItem = $this->findOne(['key' => $this->key, 'lang_code' => $this->lang_code])) && $testItem->id != $this->id) {
             $this->addError($attribute, \Yii::t('support', 'Key must be unique for selected language'));
         }
     }
 }
 /**
  * Validates the attribute using the "exist" validator
  * @param \yii\base\Model $model the data model to be validated
  * @param string $attribute the name of the attribute to be validated.
  */
 private function validateExist($model, $attribute)
 {
     if ($this->targetClass === null) {
         throw new InvalidConfigException('The "targetClass" property must be set.');
     }
     if (!is_string($this->targetAttribute)) {
         throw new InvalidConfigException('The "targetAttribute" property must be configured as a string.');
     }
     $validator = new ExistValidator();
     $validator->targetClass = $this->targetClass;
     $validator->targetAttribute = $this->targetAttribute;
     $validator->validateAttribute($model, $attribute);
 }
 public function testValidateCompositeKeys()
 {
     $val = new ExistValidator(['targetClass' => OrderItem::className(), 'targetAttribute' => ['order_id', 'item_id']]);
     // validate old record
     $m = OrderItem::findOne(['order_id' => 1, 'item_id' => 2]);
     $val->validateAttribute($m, 'order_id');
     $this->assertFalse($m->hasErrors('order_id'));
     // validate new record
     $m = new OrderItem(['order_id' => 1, 'item_id' => 2]);
     $val->validateAttribute($m, 'order_id');
     $this->assertFalse($m->hasErrors('order_id'));
     $m = new OrderItem(['order_id' => 10, 'item_id' => 2]);
     $val->validateAttribute($m, 'order_id');
     $this->assertTrue($m->hasErrors('order_id'));
     $val = new ExistValidator(['targetClass' => OrderItem::className(), 'targetAttribute' => ['id' => 'order_id']]);
     // validate old record
     $m = Order::findOne(1);
     $val->validateAttribute($m, 'id');
     $this->assertFalse($m->hasErrors('id'));
     $m = Order::findOne(1);
     $m->id = 10;
     $val->validateAttribute($m, 'id');
     $this->assertTrue($m->hasErrors('id'));
     $m = new Order(['id' => 1]);
     $val->validateAttribute($m, 'id');
     $this->assertFalse($m->hasErrors('id'));
     $m = new Order(['id' => 10]);
     $val->validateAttribute($m, 'id');
     $this->assertTrue($m->hasErrors('id'));
 }