问题
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