title: Yii2: how to enable form validation when using multiple models in a view?
body: <p>What I meet is almost like this:</p>
  
  <ol>
  <li>I have a <code>create form</code>, and the form contains two model's attribute;</li>
  <li>I pass them from controller to view, and add rules to validate attribute in both model;</li>
  <li>But the form validation did not work well - a model's validation is
  not  work.</li>
  </ol>
  
  <p>I have no idea about how to resolve this, thanks for help!</p>
  
  <p>And I find a reference article - <a href="http://www.yiiframework.com/doc-2.0/guide-input-multiple-models.html" rel="nofollow">Complex Forms with Multiple Models</a>, but it is TBD.</p>
  
  <hr>
  
  <p>Here is my sample code.</p>
  
  <p>Controller - SiteController.php:</p>
  
  <pre><code>namespace task\controllers;
  
  use yii\web\Controller;
  
  use task\models\Task;
  use task\models\Customer;
  
  class Task extends Controller
  {
  
      public function actionCreate()
      {
          $model = new Task;
          $customerModel = new Customer;
  
          // load, validate, save ...
  
          return $this-&gt;render('create', [
              'model' =&gt; $model,
              'customerModel' =&gt; $customerModel
          ]);
      }
  }
  </code></pre>
  
  <p>Model - Task.php, Customer.php:</p>
  
  <pre><code>namespace task\models;
  
  use yii\db\ActiveRecord;
  
  class Task extends AcitveRecord
  {
      public $title, $published_at;
  
      public function rules()
      {
          return [
              [['title', 'published_at'], 'required'],
              ['published_at', 'match', 'pattern' =&gt; '/patten for time/']
          ];
      }
  }
  
  namespace task\models;
  
  use yii\db\ActiveRecord;
  
  class Customer extends ActiveRecord
  {
      public $name;
  
      public function rules()
      {
          return [
              ['name', 'required'],
          ];
      }
  }
  </code></pre>
  
  <p>View - create.php:</p>
  
  <pre><code>&lt;?php
  
  use yii\helpers\Html;
  use yii\widgets\ActiveForm;
  
  ?&gt;
  
  &lt;?php $form = ActiveForm::begin(); ?&gt;
  
  &lt;?= $form-&gt;field($model, 'title')-&gt;textInput() ?&gt;
  
  &lt;?= $form-&gt;field($model, 'publish_at')-&gt;textInput() ?&gt;
  
  &lt;?= $form-&gt;field($customerModel, 'name')-&gt;textInput() ?&gt;
  
  &lt;?= Html::submitButton('submit')?&gt;
  
  &lt;?php ActiveForm::end(); ?&gt;
  </code></pre>