skip yii2 unique validation on update action if new value equals to prev value
<p>Because of unique validator get error on update action ('this username is already taken') i wanna validate username on update action when new username value not equals to prev value, it's my rule:</p>
<pre><code>        [['username'], 'unique', 'on'=&gt;'update', 'when' =&gt; function($model){
                return static::getOldUsername($model-&gt;id) !== $model-&gt;username;
            }
        ],
</code></pre>
<p>and it's my function to get prev username value:</p>
<pre><code>public static function getOldUsername($id)
{
    return static::findIdentity($id)-&gt;username;
}
</code></pre>
<p>but it doesn't work, i think $model-&gt;getId() return nothing because of with static id (e.g: 23) its work.</p>
<pre><code>        [['username'], 'unique', 'on'=&gt;'update', 'when' =&gt; function($model){
                return static::getOldUsername(23) !== $model-&gt;username;
            }
        ],
</code></pre>
<p>how can i get model id? or if you have other ways to skip yii2 unique validation on update action if new value equals to prev value, please explain it.</p>