Records are getting saved even after validation fails in yii2

点点圈 提交于 2019-12-13 08:42:56

问题


I have a model Taluka where we are supposed to select district and enter as many talukas.

But the issue is that records are getting saved even if some talukas are empty. Here is my code:

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,


            ]);
        }
    }

来源:https://stackoverflow.com/questions/43782253/records-are-getting-saved-even-after-validation-fails-in-yii2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!