/** * @inheritdoc */ public function rules() { return [['name', 'required'], [['name'], 'unique', 'message' => 'Такой город уже есть'], ['name', function ($attribute, $params) { $kladr = new Kladr(); $cities = $kladr->getCities($this->{$attribute}); foreach ($cities as $city) { if ($city === $this->{$attribute}) { return; } } $this->addError($attribute, 'Указанный город не найден в КЛАДР'); }]]; }
/** * Список городов из Кладр для автокомплита * @param null $q */ public function actionCityList($q = null) { Yii::$app->response->format = Response::FORMAT_JSON; $result = ['results' => [['id' => '', 'text' => '']]]; if ($q != null) { $kladr = new Kladr(); $cities = $kladr->getCities($q); if ($cities) { $results = []; foreach ($cities as $city) { $results[] = ['id' => $city, 'text' => $city]; } if (count($results)) { $result['results'] = $results; } } } return $result; }