2016-12-03 4 views
0

私はwbragancaの動的フォームを使用していますが、次のエラーが発生しています。 できるだけ私を助けてください。yii2の動的フォームに助けてくれる人

  1. データを挿入して[作成]ボタンをクリックすると、表示ページが表示されず、単純な空白のページが表示されます。このビューページは
  2. データベースストアヌル値
  3. のGridView displayesが表示されないように "[設定されていない" 値

form.php

use yii\helpers\Html; 
use yii\widgets\ActiveForm; 
use yii\helpers\ArrayHelper; 
use frontend\models\Items; 
use frontend\models\Employees; 
use frontend\models\Departments; 
use dosamigos\datepicker\DatePicker; 
use wbraganca\dynamicform\DynamicFormWidget; 
/* @var $this yii\web\View */ 
/* @var $model backend\models\Borrow */ 
/* @var $form yii\widgets\ActiveForm */ 
    $js = ' 
    jQuery(".dynamicform_wrapper").on("afterInsert", function(e, item) { 

    jQuery(".dynamicform_wrapper .panel-title- address").each(function(index) { 

    jQuery(this).html("Borrows: " + (index + 1)) 
    }); 

    }); 


    jQuery(".dynamicform_wrapper").on("afterDelete", function(e) { 

    jQuery(".dynamicform_wrapper .panel-title-address").each(function(index)  
    { 

     jQuery(this).html("Borrows: " + (index + 1)) 

    }); 

    }); 

    '; 

    $this->registerJs($js); 
    ?> 
    <div class="borrow-form"> 
    <?php $form = ActiveForm::begin(['id'=>'dynamic-form']); ?> 
     <div class="row"> 
     <div class="col-xs-4"> 
     <?= $form->field($model,'dept_id')->dropDownList(
      ArrayHelper::map(Departments::find()->all(),'id','dept_name'), 
      ['prompt'=>'select departments']) 
     ?> 
    </div> 
    <div class="col-xs-4"> 
     <?=$form->field($model, 'return_date')->widget(
      DatePicker::className(), [ 
      'inline' => false, 
      'clientOptions' => [ 
      'autoclose' => true, 
      'format' => 'yyyy-mm-dd' 
      ] 
     ]);?> 
    </div> 
    </div> 
    <div class="padding-v-md"> 

    <div class="line line-dashed"></div> 

    </div> 
    <!-- beginning of dynamic form --> 
    <?php DynamicFormWidget::begin([ 
    'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_] 
    'widgetBody' => '.container-items', // required: css class selector 
    'widgetItem' => '.item', // required: css class 
    'limit' => 10, // the maximum times, an element can be added (default 999) 
    'min' => 1, // 0 or 1 (default 1) 
    'insertButton' => '.add-item', // css class 
    'deleteButton' => '.remove-item', // css class 
    'model' => $modelsAddress[0], 
    'formId' => 'dynamic-form', 
    'formFields' => [ 
    'items_id', 
    'unit', 
    'request', 
    'allowed', 
    ], 
    ]); ?> 
    <div class="panel panel-default"> 
    <div class="panel-heading"> 
    <h4><i class="glyphicon glyphicon-envelope"></i> Items    
    </h4> 
    </div> 
    <div class="panel-body"> 
    <div class="container-items"><!-- widgetBody --> 
    <?php foreach ($modelsAddress as $i => $modelAddress): ?> 
     <div class="item panel panel-default"><!-- widgetItem --> 
      <div class="panel-heading"> 
       <h3 class="panel-title pull-left">Items</h3> 
       <div class="pull-right"> 
        <button type="button" class="add-item btn btn-success btn-xs"><i class="glyphicon glyphicon-plus"></i></button> 
        <button type="button" class="remove-item btn btn-danger btn-xs"><i class="glyphicon glyphicon-minus"></i></button> 
       </div> 
       <div class="clearfix"></div> 
      </div> 
      <div class="panel-body"> 
       <?php 
        // necessary for update action. 
        if (! $modelAddress->isNewRecord) { 
         echo Html::activeHiddenInput($modelAddress, "[{$i}]id"); 
        } 
       ?> 

       <div class="row"> 
        <div class="col-xs-4"> 
         <?= $form->field($modelAddress, "[{$i}]items_id")->dropDownList(        ArrayHelper::map(Items::find()->all(),'id','item_name'), 
         ['prompt'=>'select items']) ?> 
        </div> 
        <div class="col-xs-2"> 
         <?= $form->field($modelAddress, "[{$i}]unit")->textInput(['maxlength' => true]) ?> 
        </div> 
        <div class="col-xs-2"> 
         <?= $form->field($modelAddress, "[{$i}]request")->textInput(['maxlength' => true]) ?> 
        </div>       

        <div class="col-xs-2"> 
         <?= $form->field($modelAddress, "[{$i}]allowed")->textInput(['maxlength' => true]) ?> 
        </div> 
        <div class="col-xs-2"> 
         <?= $form->field($modelAddress, "[{$i}]unit_price")->textInput(['maxlength' => true]) ?> 

        </div> 
       </div><!-- .row --> 
      </div> 
     </div> 
     <?php endforeach; ?> 
     </div> 
    </div> 
    </div><!-- .panel --> 
    <?php DynamicFormWidget::end(); ?> 
    <!-- end dynamic form--> 

    <div class="row"> 
    <div class="col-xs-5">      <?=$form->field($model,'emp_id')->dropDownList(      ArrayHelper::map(Employees::find()->all(),'id','emp_name'), 
       ['prompt'=>'select employees']) 
     ?> 
     <?= $form->field($model,'head_id')->dropDownList(
        ArrayHelper::map(Employees::find()->all(),'id','emp_name'), 
       ['prompt'=>'select dept heads']) 
     ?> 
     <?= $form->field($model,'man_id')->dropDownList(
       ArrayHelper::map(Employees::find()->all(),'id','emp_name'), 
       ['prompt'=>'select stoke managers']) 
     ?> 
     <?= $form->field($model,'keeper_id')->dropDownList(
       ArrayHelper::map(Employees::find()->all(),'id','emp_name'), 
       ['prompt'=>'select stoke keepers']) 
     ?> 
    </div> 
    <div class="col-xs-5"> 

     <?=$form->field($model, 'emp_date')->widget(
      DatePicker::className(), [ 
      'inline' => false, 
      'clientOptions' => [ 
      'autoclose' => true, 
      'format' => 'yyyy-mm-dd' 
      ] 
     ]);?> 

     <?=$form->field($model, 'head_date')->widget(
      DatePicker::className(), [ 
       'inline' => false, 
       'clientOptions' => [ 
       'autoclose' => true, 
       'format' => 'yyyy-mm-dd' 
       ] 
     ]);?> 

     <?=$form->field($model, 'man_date')->widget(
      DatePicker::className(), [ 
       'inline' => false, 
       'clientOptions' => [ 
       'autoclose' => true, 
       'format' => 'yyyy-mm-dd' 
       ] 
     ]);?> 

     <?=$form->field($model, 'keeper_date')->widget(
      DatePicker::className(), [ 
       'inline' => false, 
       'clientOptions' => [ 
       'autoclose' => true, 
       'format' => 'yyyy-mm-dd' 
       ] 
     ]);?> 
     </div>     
     </div> 
    <div class="form-group"> 
    <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> 
    </div> 

    <?php ActiveForm::end(); ?> 

controller.php

namespace frontend\controllers; 
    use Yii; 
    use frontend\models\Borrow; 
    use frontend\models\BorrowSearch; 
    use yii\web\Controller; 
    use yii\web\NotFoundHttpException; 
    use yii\filters\VerbFilter; 
    use frontend\models\Items; 
    use frontend\models\Model; 
    /** 
    * BorrowController implements the CRUD actions for Borrow model. 
    */ 
    class BorrowController extends Controller 
    { 
    /** 
    * @inheritdoc 
    */ 
    public function behaviors() 
    { 
    return [ 
     'verbs' => [ 
     'class' => VerbFilter::className(), 
     'actions' => [ 
      'delete' => ['POST'], 
     ], 
    ], 
    ]; 
    } 

    /** 
    * Lists all Borrow models. 
    * @return mixed 
    */ 
    public function actionIndex() 
    { 
    $searchModel = new BorrowSearch(); 
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams); 

    return $this->render('index', [ 
    'searchModel' => $searchModel, 
    'dataProvider' => $dataProvider, 
    ]); 
    } 

/** 
* Displays a single Borrow model. 
* @param integer $id 
* @return mixed 
*/ 
public function actionView($id) 
{ 
    return $this->render('view', [ 
    'model' => $this->findModel($id), 
    ]); 
} 

/** 
    * Creates a new Borrow model. 
    * If creation is successful, the browser will be redirected to the 'view' page. 
* @return mixed 
*/ 
public function actionCreate() 
{ 
    $model = new Borrow(); 
    $modelsAddress = [new Borrow]; 
    if ($model->load(Yii::$app->request->post())&& $model->save()) 
    { 

     $modelsAddress = Model::createMultiple(Borrow::classname()); 

     Model::loadMultiple($modelsAddress, Yii::$app->request->post()); 
    // validate all models 

    $valid = $model->validate(); 

    $valid = Model::validateMultiple($modelsAddress) && $valid; 


    if ($valid) { 

     $transaction = \Yii::$app->db->beginTransaction(); 

     try { 

      if ($flag = $model->save(false)) 
       { 

        foreach ($modelsAddress as $modelAddress) 
         { 
          $modelAddress->items_id = $model->items_id; 

          if (! ($flag = $modelAddress->save(false))) 
           { 
            $transaction->rollBack(); 

            break; 
           } 
         } 

       } 

      if ($flag) 
       { 
        $transaction->commit(); 

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

     } catch (Exception $e) { 

      $transaction->rollBack(); 

     } 

     } 

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

    'model' => $model, 

    'modelsAddress' => (empty($modelsAddress)) ? [new Borrow] : $modelsAddress 

    ]); 

    } 
    } 
    /** 
    * Updates an existing Borrow model. 
    * If update is successful, the browser will be redirected to the 'view' page. 
    * @param integer $id 
    * @return mixed 
*/ 
    public function actionUpdate($id) 
    { 
    $model = $this->findModel($id); 
    $modelsAddress = $model->items; 

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


     $oldIDs = ArrayHelper::map($modelsAddress, 'id', 'id'); 

     $modelsAddress = Model::createMultiple(Borrow::classname(), $modelsAddress); 

     Model::loadMultiple($modelsAddress, Yii::$app->request->post()); 

     $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsAddress, 'id', 'id'))); 
     // validate all models 

     $valid = $model->validate(); 

     $valid = Model::validateMultiple($modelsAddress) && $valid; 

     if ($valid) 
      { 

       $transaction = \Yii::$app->db->beginTransaction(); 

       try 
        { 

         if ($flag = $model->save(false)) 
          { 

           if (!empty($deletedIDs)) 
            { 

             Borrow::deleteAll(['id' => $deletedIDs]); 

            } 

           foreach ($modelsAddress as $modelAddress) 
            { 

             $modelAddress->items_id = $model->id; 

             if (! ($flag = $modelAddress->save(false))) 
              { 

               $transaction->rollBack(); 
               break; 
              }        
            } 
          } 

         if ($flag) 
          { 
           $transaction->commit(); 
           return $this->redirect(['view', 'id' => $model->id]); 
          } 

        } catch (Exception $e) { 
         $transaction->rollBack(); 

       } 

      } 

     } 

    else 
     { 
     return $this->render('update', [ 

      'modelCustomer' => $model, 

      'modelsAddress' => (empty($modelsAddress)) ? [new Borrow] : $modelsAddress 
     ]); 
    } 
} 

    /** 
    * Deletes an existing Borrow model. 
    * If deletion is successful, the browser will be redirected to the 'index' page. 
    * @param integer $id 
    * @return mixed 
    */ 
public function actionDelete($id) 
{ 
    $this->findModel($id)->delete(); 

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

    /** 
    * Finds the Borrow model based on its primary key value. 
    * If the model is not found, a 404 HTTP exception will be thrown. 
    * @param integer $id 
    * @return Borrow the loaded model 
    * @throws NotFoundHttpException if the model cannot be found 
    */ 
protected function findModel($id) 
{ 
    if (($model = Borrow::findOne($id)) !== null) { 
    return $model; 
} else { 
    throw new NotFoundHttpException('The requested page does not exist.'); 
    } 
} 
} 
+0

okあなたのリクエストがコントローラに到達したかどうかを確認してください。あなたの 'actionCreate'で' die( 'hello'); 'を使ってください。これを試して、あなたがここに到達しているかどうか知らせてください。ここに来たら、die( 'hello')を使ってください。これにより、問題がどこで起こっているのかがわかります。 –

答えて

0

私はあなたの質問にコメントするには十分な評判を持っていません。なぜ私は答えを書く必要があるのですかここに。

私は問題があなたのコントローラの作成と更新操作にあると思います。

if($flag){ 
    $transaction->commit(); 
    return $this->redirect(['view', 'id' => $model->id]); 
    } 

ここには条件がありません。 正確な問題は何かを得るには、以下のようにelse条件を入れてください。

if ($flag) 
    { 
    $transaction->commit(); 
    return $this->redirect(['view', 'id' => $model->id]); 
    }else { 
return $this->render('update', [ 

     'modelCustomer' => $model, 

     'modelsAddress' => (empty($modelsAddress)) ? [new Borrow] : $modelsAddress 
    ]); 
} 
+0

あなたの貴重な助けに感謝します。私はあなたが私に言ったように努力しました。しかし、何も変わりません。問題はいつも通りです。もっと手伝ってください。再度、感謝します。 – hafhag

+0

あなたは本当ですか?それを両方のアクションに貼り付けますか? –

+0

確かに、私はそれを作ったが、まだ変更はなかった。 – hafhag

関連する問題