Summing up in the comments...
Most probably what you need is to lock table manually before validation and release lock after it. Yii2 provides optimistic locks mechanism but it is not suitable for your case. Optimistic locks are supported only inside update and delete methods:
Optimistic locking is only supported when you update or delete an
existing row of data using yii\db\ActiveRecord::update() or
yii\db\ActiveRecord::delete(), respectively.
Moreover what does optimistic lock is just raise exception when update fails due conflict (no actual table locking).
The solution will depends on your DB engine. Yii2 provides mutex mechanism for manual locking. Out of the box Yii2 supported Mysql and Postgres. See components description at following pages of Yii2 manual :
So, after you configured your mutex in config (example for pgsql from official guide):
[
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'pgsql:host=127.0.0.1;dbname=demo',
]
'mutex' => [
'class' => 'yii\mutex\PgsqlMutex',
],
],
]
you'll need to do something like that
\Yii::$app->mutex->acquireLock($lockingObject);
// validate uniqueness and save
\Yii::$app->mutex->releaseLock($lockingObject);
Or, for sure you can do it manually using SQL syntax of your RDBMS.
MySQL:
SELECT GET_LOCK('tablename',10);
SELECT RELEASE_LOCK('tablename');
Pqsql:
LOCK TABLE tablename IN SHARE ROW EXCLUSIVE MODE;
Be aware that Pgsql lock works only inside transaction.