title: Yii2 Validate multiple models
body: <p>I have two models in Yii2 (masterTransaction and splitTransaction), where each masterTransactions can have multiple splitTransactions. Each splitTransaction has an attribute 'amount'. My problem is I need to validate if the sum over all 'amount' attributes is 0. </p>
  
  <p>My first solution was to make another model called Transaction, in which I had an attribute where I saved an instance of the masterTransaction model and another attribute with an array of splitTransaction instances. I did the validation with a custom inline validatior, which work perfectly.</p>
  
  <h2>Transaction model</h2>
  
  <pre><code>class Transaction extends Model
  {
      public $masterTransaction;
      public $splitTransactions;
  
      public function init()
      {
          $this-&gt;masterTransaction = new MasterTransaction();
          $this-&gt;splitTransactions[] = new SplitTransaction();
      }
  
      public function rules()
      {
          return [
              ['splitTransactions', 'validateSplitTransactions'],
          ];
      }
  
      public function validateSplitTransactions($attribute, $params)
      {
          $sum = 0;
          foreach ($this-&gt;$attribute as $transaction) {
              $sum = bcadd($sum, $transaction-&gt;amount, 3);
          }
          if ($sum != 0) {
              $this-&gt;addError($attribute, 'The sum of the entries has to be 0');
          }
      }
  
      public function save()
      {
          $this-&gt;masterTransaction-&gt;save();
          foreach ($this-&gt;splitTransactions as $splitTransaction) {
              $splitTransaction-&gt;master_transaction_id = $this-&gt;masterTransaction-&gt;id;
              $splitTransaction-&gt;save();
          }
      }
  }
  </code></pre>
  
  <h2>Controller function to create the model</h2>
  
  <pre><code>public function actionCreate()
  {
      $transaction = new Transaction();
      $count = count(Yii::$app-&gt;request-&gt;post('SplitTransaction', []));
      for ($i = 1; $i &lt; $count; $i++) {
          $transaction-&gt;splitTransactions[] = new SplitTransaction();
      }
  
      if ($transaction-&gt;masterTransaction-&gt;load(Yii::$app-&gt;request-&gt;post()) &amp;&amp; Model::loadMultiple($transaction-&gt;splitTransactions, Yii::$app-&gt;request-&gt;post())) {
          $transaction-&gt;masterTransaction-&gt;user_id = Yii::$app-&gt;user-&gt;id;
          foreach ($transaction-&gt;splitTransactions as $splitTransaction) {
              $splitTransaction-&gt;user_id = Yii::$app-&gt;user-&gt;id;
          }
          if ($transaction-&gt;validate()) {
              $transaction-&gt;save();
          }
      }
  
      return $this-&gt;render('create', [
          'transaction' =&gt; $transaction,
      ]);
  }
  </code></pre>
  
  <p>But when I tried building a form to input the data, I ran into a problem with the Ajax validation. The validation would work, but Yii didn't know where to put the error message, so it just deleted it.</p>
  
  <p>I suspect that this is just not the preferred way in Yii2 model my data, but I don't really have another idea. Maybe someone has some ideas for me.</p>