2017-04-07 73 views
0

我提交我的請求後,它會變成空白頁。請檢查我的代碼。謝謝。 我使用yii2框架和wbraganca動態表單插件。yii2 - dynamicform wbraganca:我的代碼出了什麼問題

public function actionCreate() 
    { 
     $model = new PurchaseOrder(); 
     $details = [ new PurchaseOrderDetail ]; 

     $seq = Sequence::findOne(['id' => 'PO', 'name' => (int)date('ymd')]); 
     if(is_null($seq)){ 
      $_seq = new Sequence(); 
      $_seq->id = 'PO'; 
      $_seq->name = (int)date('ymd'); 
      $_seq->value = 0; 
      $_seq->save(); 
      $model->id = $_seq->id . '/' . $_seq->name . '/' .str_pad($_seq->value+1, 3, "0", STR_PAD_LEFT); 
     } 
     else { 
      $seq->value += 1; 
      $model->id = $seq->id . '/' . $seq->name . '/' . str_pad($seq->value, 3, "0", STR_PAD_LEFT); 
      $seq->update(); 
     } 
     $model->trx_date = date('Y-m-d'); 

     if ($model->load(Yii::$app->request->post())) { 
      $details = Model::createMultiple(PurchaseOrderDetail::classname()); 
      Model::loadMultiple($details, Yii::$app->request->post()); 

      //assign po_id 
      foreach($details as $detail){ 
       $detail->oid = uniqid(); 
       $detail->po_id = 0; 
      } 

      // ajax validation 
      if(Yii::$app->request->isAjax){ 
       Yii::$app->response->format = Response::FORMAT_JSON; 
       return ArrayHelper::merge(
        ActiveForm::validateMultiple($details), 
        ActiveForm::validate($model) 
       ); 
      } 

      // validate all models 
      $valid1 = $model->validate(); 
      $valid2 = Model::validateMultiple($details); 
      $valid = $valid1 && $valid2; 

      if($valid){ 
       $trx = \Yii::$app->db->beginTransaction(); 
       try 
       { 
        // save master record 
        if($flag = $model->save(false)){ 
         // save detail records 
         foreach($details as $detail){ 
          $detail->po_id = $model->id; 
          if(!($flag = $detail->save(false))){ 
           $trx->rollBack(); 
           break; 
          } 
         }      
        } 
        if($flag){ 
         //if success commit transaction 
         //then view the result 
         $trx->commit(); 
         return $this->redirect(['view', 'id' => $model->id]); 
        } else { 
         return $this->render('create', [ 
          'model' => $model, 
          'details' => $details 
         ]); 
        } 
       } 
       catch(Exception $ex){ 
        // transaction failed, rollBack 
        $trx->rollBack(); 
        throw $ex; 
       } 
      }      
     } else { 
      return $this->render('create', [ 
       'model' => $model, 
       'details' => $details 
      ]); 
     } 
    } 

當我點擊創建按鈕,它重定向到空白頁,我檢查日誌,它沒有生成任何錯誤日誌。這裏是日誌

Routing 
Name Value 
Route 'purchase-order/create' 
Action 'app\\controllers\\PurchaseOrderController::actionCreate()' 
Parameters [] 
$_GET 
Empty. 

$_POST 
Name Value 
_csrf 'a0hCeHJoLXEbEgY.C15vXD0ydBEiLVsVPSQ2OUQSdQs0MCoTHw1qQg==' 
PurchaseOrder [ 
    'id' => 'PO/170407/024' 
    'trx_date' => '2017-04-07' 
    'supplier_id' => '1' 
    'remark' => 'test remark' 
    'total' => '1500000' 
] 
PurchaseOrderDetail [ 
    0 => [ 
     'item_id' => '1' 
     'qty' => '10' 
     'price' => '150000' 
     'total' => '1500000' 
     'remark' => 'aa' 
    ] 
] 
$_FILES 
Empty. 

$_COOKIE 
Name Value 
_csrf 'c3c290ca6c2fdb8a905c1692baebc21ba6a5715325855d6e863a91f1cd5cd5c7a:2:{i:0;s:5:\"_csrf\";i:1;s:32:\"pZDFy6B-Vz6iPEvdVltA6zXz_xhkmeG3\";}' 
PHPSESSID 'o1337j64mptbjhptr2o3dcl5k7' 
Request Body 
Name Value 
Content Type 'application/x-www-form-urlencoded' 
Raw '_csrf=a0hCeHJoLXEbEgY.C15vXD0ydBEiLVsVPSQ2OUQSdQs0MCoTHw1qQg%3D%3D&PurchaseOrder%5Bid%5D=PO%2F170407%2F024&PurchaseOrder%5Btrx_date%5D=2017-04-07&PurchaseOrder%5Bsupplier_id%5D=1&PurchaseOrder%5Bremark%5D=test+remark&PurchaseOrderDetail%5B0%5D%5Bitem_id%5D=1&PurchaseOrderDetail%5B0%5D%5Bqty%5D=10&PurchaseOrderDetail%5B0%5D%5Bprice%5D=150000&PurchaseOrderDetail%5B0%5D%5Btotal%5D=1500000&PurchaseOrderDetail%5B0%5D%5Bremark%5D=aa&PurchaseOrder%5Btotal%5D=1500000' 
Decoded to Params [ 
    '_csrf' => 'a0hCeHJoLXEbEgY.C15vXD0ydBEiLVsVPSQ2OUQSdQs0MCoTHw1qQg==' 
    'PurchaseOrder' => [ 
     'id' => 'PO/170407/024' 
     'trx_date' => '2017-04-07' 
     'supplier_id' => '1' 
     'remark' => 'test remark' 
     'total' => '1500000' 
    ] 
    'PurchaseOrderDetail' => [ 
     0 => [ 
      'item_id' => '1' 
      'qty' => '10' 
      'price' => '150000' 
      'total' => '1500000' 
      'remark' => 'aa' 
     ] 
    ] 
] 

請幫助。前

+0

外鍵跳過驗證已經解決謝謝,我只是發現了爲什麼驗證錯誤 –

+0

這個問題已經解決了這個錯誤。 –

回答

0

這個問題,通過這樣的

[['item_id'], 'exist', 'skipOnError' => true, 'targetClass' => Item::className(), 'targetAttribute' => ['item_id' => 'id']], 
相關問題