2016-03-05 97 views
-1

之前保存我使用yii2的稱重橋項目驗證內容yii2

一旦創建,用戶會被重定向到查看,但我的控制器不,即使數據不是這樣的方式驗證信息輸入表單字段中,用戶總是被重定向到查看。

我怎樣才能實現驗證屬性

控制器代碼:

public function actionCreate() 
{ 
    $model = new TruckWeight1(); 

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

     $model->time_recorded =date('H:i:s');; 
     $model->recorded_by = 
     $model->recorded_date = date('Y-m-d'); 

     $model->save(); 

     return $this->redirect(['view', 'id' => $model->id]); 

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

回答

1

試試這個

public function actionCreate() 
{ 
    $model = new TruckWeight1(); 

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

     $model->time_recorded =date('H:i:s');; 
     $model->recorded_by = 
     $model->recorded_date = date('Y-m-d'); 

     $model->save(); 

     return $this->redirect(['view', 'id' => $model->id]); 

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

更多關於驗證validation

+0

接受它,然後@GEOFFREYMWANGI – Bloodhound