即使在yii2中验证失败,记录也会被保存

时间:2017-05-04 11:56:54

标签: yii2 yii2-basic-app

我有一个模型Taluka,我们应该选择区域并输入尽可能多的talukas。

但问题是即使一些talukas是空的,记录也会被保存。 这是我的代码:

public function actionCreate()
    {

      $model = new Taluka();

      if ($model->load(Yii::$app->request->post()) ) {

                $talukaslist = $model->talukas;

                if(is_array($talukaslist))
                {

                    foreach($talukaslist as $taluka)
                     {
                         if($taluka == null)
                         {
                                  return $this->render('create', [
                                    'model' => $model,
                                  ]);
                         }
                         else
                          if($taluka!=null)
                          {
                           $talukaRecord = new Taluka();
                           $talukaRecord->DistrictId = $model->DistrictId;
                           $talukaRecord->Taluka = $taluka;
                           $talukaRecord->save(false);
                          }
                     }
                }

                 return $this->redirect(['index']);

      }  
         else {
            return $this->render('create', [
               'model' => $model,


            ]);
        }
    }

0 个答案:

没有答案