title: Yii2 form update - validation based on related model attribute
body: <p>I have a form, where the first step is to choose city (City is a related model and has an attribute of population min. and max. e.g. 10.000 - 12.000). In the 2nd step the user has to fill population (e.g. 11.000). Now in rules I'm checking if the user has filled the right number of population (if it's between min. and max. population of City).
  Create new record is working. But I have a problem with Update action. Because if I already have a number in population (11.000) and I have a <code>City</code> attached and I would like to change city to something else where population should be e.g. between 5.000 and 7.000, validation of population in my model fails, because it is validated based on the <em>old</em> City (of course until all values pass the validation <code>city_id</code> won't be updated). Can you please point me to the right direction? Should I maybe update <code>city_id</code> in my model before validation (is it possible)? Thank you!</p>
  
  <p>form:</p>
  
  <pre><code>&lt;?= $form-&gt;field($model, 'city_id')-&gt;dropDownList(
      \yii\helpers\ArrayHelper::map(app\models\City::find()-&gt;all(), 'id', 'name')); ?&gt;
  &lt;?= $form-&gt;field($model, 'population')-&gt;textInput(['maxlength' =&gt; true]) ?&gt;
  </code></pre>
  
  <p>model rules:</p>
  
  <pre><code>...
  ['population', 'validatePopulation'],
  ...
  
  public function getCity() {
      return $this-&gt;hasOne(\app\models\City::className(), ['id' =&gt; 'city_id']);
  }
  
  public function validatePopulation($attribute, $params, $validator) {
      if ($this-&gt;population &lt; $this-&gt;city-&gt;population_min) {
          $this-&gt;addError($attribute, 'Population has to be larger than ' . $this-&gt;city-&gt;population_min);
      } elseif ($this-&gt;population &gt; $this-&gt;city-&gt;population_max) {
          $this-&gt;addError($attribute, 'Population has to be smaller than ' . $this-&gt;city-&gt;population_max);
      }
  }
  </code></pre>