title: Yii2 multiple forms in a single action
body: <p>
  Which is the right way to handle multiple forms in a single action?</p>
  
  <p>Here is my models/MembersBans.php</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  namespace app\models;
  
  use Yii;
  use yii\behaviors\TimestampBehavior;
  use app\models\Members;
  
  class MembersBans extends \yii\db\ActiveRecord {
  
  public $username;
  
  public static function tableName() {
      return '{{%members_bans}}';
  }
  
  public static function primaryKey() {
      return array('ban_id');
  }
  
  public function behaviors() {
      return [
          [
              'class' =&gt; TimestampBehavior::className(),
              'createdAtAttribute' =&gt; 'date_added',
              'updatedAtAttribute' =&gt; 'last_updated',
          ],
      ];
  }
  
  public function rules() {
      return [
          [['ban_id', 'ban_memberid', 'date_added', 'last_updated'], 'integer'],
          [['username', 'end_date'], 'safe'],
          ['end_date', 'date', 'format' =&gt; 'yyyy-mm-dd'],
          [['ban_ip'], 'string', 'max' =&gt; 40],
          [['reason'], 'string', 'max' =&gt; 255]
      ];
  }
  
  public function attributeLabels() {
      return [
          'ban_id' =&gt; Yii::t('app', 'ID на бана'),
          'ban_memberid' =&gt; Yii::t('app', 'Потребителско ID'),
          'username' =&gt; 'Потребителско име',
          'ban_ip' =&gt; Yii::t('app', 'IP адрес'),
          'end_date' =&gt; Yii::t('app', 'Дата на изтичане'),
          'reason' =&gt; Yii::t('app', 'Причина за бана'),
          'date_added' =&gt; Yii::t('app', 'Дата на добавяне'),
      ];
  }
  
  public function getMemberBans() {
      $bans = $this-&gt;find()-&gt;where('ban_memberid');
      return $bans;
  }
  
  public function getIpBans() {
      $bans = $this-&gt;find()-&gt;where('ban_ip');
      return $bans;
  }
  
  public function getMember() {
      return $this-&gt;hasOne(Members::className(), ['member_id' =&gt; 'ban_memberid']);
  }
  
  public function banMember() {
      $memberInfo = Members::findByUsername($this-&gt;username);
      if ($memberInfo) {
          $this-&gt;ban_memberid = $memberInfo-&gt;member_id;
          if ($this-&gt;save()) {
              Yii::$app-&gt;session-&gt;setFlash('alert-success', 'Потребителят беше успешно блокиран.');
          } else {
              Yii::$app-&gt;session-&gt;setFlash('alert-error', 'Възникна грешка при блокирането на потребителя.');
          }
      } else {
          Yii::$app-&gt;session-&gt;setFlash('alert-error', 'Не съществува потребител с това потребителско име.');
      }
  }
  
  public function banIp() {
      if ($this-&gt;save()) {
          Yii::$app-&gt;session-&gt;setFlash('alert-success', 'IP адресът беше успешно блокиран.');
      } else {
          Yii::$app-&gt;session-&gt;setFlash('alert-error', 'Възникна грешка при блокирането на IP адреса.');
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>My controllers/MembersBansController.php:</p>
  
  <pre class="lang-php prettyprint-override"><code>public function actionList() {
      $membersBans = new MembersBans();
      if ($membersBans-&gt;load(Yii::$app-&gt;request-&gt;post('banMember'))) {
          $membersBans-&gt;banMember();
      }
      if ($membersBans-&gt;load(Yii::$app-&gt;request-&gt;post('banIp'))) {
          $membersBans-&gt;banIp();
      }
      return $this-&gt;render('list', [
                  'membersBans' =&gt; $membersBans,
      ]);
  }
  </code></pre>
  
  <p>views/members-bans/list:</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;div class="the-box"&gt;
      &lt;?php
      $activeForm = ActiveForm::begin([
          'id'                     =&gt; 'banMember',
          'enableClientValidation' =&gt; true,
          'enableAjaxValidation'   =&gt; true,
          'validateOnSubmit'       =&gt; true,
          'validateOnChange'       =&gt; true,
          'validateOnType'         =&gt; true,
      ])
      ?&gt;
      &lt;?= $activeForm-&gt;field($membersBans, 'username', [
          'template' =&gt; '{label}{input}{hint}{error}'
      ])-&gt;textInput(['class' =&gt; 'form-control', 'placeholder' =&gt; 'Въведете потребителско име']);
      ?&gt;
      &lt;?= $activeForm-&gt;field($membersBans, 'end_date', [
          'template' =&gt; '{label}{input}{hint}{error}'
      ])-&gt;textInput(['class' =&gt; 'form-control datepicker', 'placeholder' =&gt; 'Въведете период на бана']);
      ?&gt;
      &lt;?= $activeForm-&gt;field($membersBans, 'reason', [
          'template' =&gt; '{label}{input}{hint}{error}'
      ])-&gt;textInput(['class' =&gt; 'form-control', 'placeholder' =&gt; 'Въведете причина за бана']);
      ?&gt;
      &lt;div class="form-group"&gt;
          &lt;?= Html::submitButton('Добави', ['type' =&gt; 'submit', 'class' =&gt; 'btn btn-default']) ?&gt;
      &lt;/div&gt;
      &lt;?php ActiveForm::end()?&gt;
  &lt;/div&gt;
  
  
  &lt;div class="the-box"&gt;
      &lt;?php
      $activeForm = ActiveForm::begin([
          'id'                     =&gt; 'banIp',
          'enableClientValidation' =&gt; true,
          'enableAjaxValidation'   =&gt; true,
          'validateOnSubmit'       =&gt; true,
          'validateOnChange'       =&gt; true,
          'validateOnType'         =&gt; true,
      ])
      ?&gt;
      &lt;?= $activeForm-&gt;field($membersBans, 'ban_ip', [
          'template' =&gt; '{label}{input}{hint}{error}'
      ])-&gt;textInput(['class' =&gt; 'form-control', 'placeholder' =&gt; 'Въведете IP адрес или цяла мрежа']);
      ?&gt;
      &lt;?= $activeForm-&gt;field($membersBans, 'end_date', [
          'template' =&gt; '{label}{input}{hint}{error}'
      ])-&gt;textInput(['class' =&gt; 'form-control datepicker', 'placeholder' =&gt; 'Въведете период на бана']);
      ?&gt;
      &lt;?= $activeForm-&gt;field($membersBans, 'reason', [
          'template' =&gt; '{label}{input}{hint}{error}'
      ])-&gt;textInput(['class' =&gt; 'form-control', 'placeholder' =&gt; 'Въведете причина за бана']);
      ?&gt;
      &lt;div class="form-group"&gt;
          &lt;?= Html::submitButton('Добави', ['type' =&gt; 'submit', 'class' =&gt; 'btn btn-default']) ?&gt;
      &lt;/div&gt;
      &lt;?php ActiveForm::end() ?&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>And it doesn't seem to work. Any ideas?</p>