tsatsar Posted September 5, 2016 Share Posted September 5, 2016 (edited) Hello, I'm using yii2 since one month and I'm blocked. I need to create multiple instances of a table "Feuille_de_jour_responsable" based on dropdown list that search data on 3 other tables (the table feuille_de_jour_responsable have the ID of other tables as secondary key). The number of intances that must be saved depends on the number of datepicker selected (I use de kartik's datepicker). I have an error and I don't understand... "Either the 'formName' has to be set or a valid 'model' property must be set extending from '\yii\base\Model'." I try soooo many things ! But nothing work. There is my actionCreate functun on my controller code : public function actionCreate() { $count = count(Yii::$app->request->post('FeuilleDeJourResponsable', [])); $array_feuille_de_jour_responsable = [new FeuilleDeJourResponsable()]; for($i = 1; $i < $count; $i++) { $array_feuille_de_jour_responsable[] = new FeuilleDeJourResponsable(); } if (FeuilleDeJourResponsable::loadMultiple($array_feuille_de_jour_responsable,Yii::$app->request->post()) ) { foreach ($array_feuille_de_jour_responsable as $feuille) { $feuille->save(false); } return $this->redirect('index'); } else { return $this->render('create', [ 'feuille_de_jour_responsable' => $array_feuille_de_jour_responsable, ]); } } And my create view : <?php use yii\helpers\Html; use yii\helpers\ArrayHelper; use app\models\FeuilleDeJourResponsable; use app\models\PosteFdj; use app\models\Personnel; use app\models\CategorieFdj; use app\models\MeteoPrevision; use kartik\widgets\ActiveForm; use kartik\builder\Form; use kartik\builder\FormGrid; use kartik\builder\TabularForm; use kartik\widgets\DatePicker; $this->title = 'Création Feuille De Jour Responsable'; $this->params['breadcrumbs'][] = ['label' => 'Feuille De Jour Responsables', 'url' => ['index']]; $this->params['breadcrumbs'][] = $this->title; setlocale (LC_TIME, 'fr_FR.utf8','fra'); $dateDemain = ucfirst(strftime("%Y-%m-%d" , strtotime("+1 day"))); ?> <div class="feuille-de-jour-responsable-create"> <?php $reqNomPoste = 'SELECT Nom_Poste_FDJ,ID_Poste_FDJ FROM poste_fdj'; $nomPoste = PosteFdj::findBySql($reqNomPoste) ->asArray() ->all(); //var_dump($nomPoste); $reqNomPersonnel = 'SELECT Nom_Personnel,Code_Personnel FROM personnel'; $nomPersonnel = Personnel::findBySql($reqNomPersonnel) ->asArray() ->all(); //var_dump($nomPersonnel); $reqCategorie = 'SELECT Nom,ID_Categorie FROM categorie_fdj'; $categorie = CategorieFdj::findBySql($reqCategorie) ->asArray() ->all(); //var_dump($nomPersonnel); $form = ActiveForm::begin(); echo FormGrid::widget([ 'model'=>$feuille_de_jour_responsable, 'form'=>$form, 'autoGenerateColumns'=>true, 'rows'=>[ [ 'attributes'=>[ 'ID_Poste_FDJ'=>['type'=>Form::INPUT_DROPDOWN_LIST, 'items'=>$nomPoste, 'hint'=>'Choisir poste'], 'Code_Personnel'=>['type'=>Form::INPUT_DROPDOWN_LIST, 'items'=>$nomPersonnel, 'hint'=>'Choisir Responsable'], 'ID_Categorie'=>['type'=>Form::INPUT_DROPDOWN_LIST, 'items'=>$categorie, 'hint'=>'Choisir categorie'], ] ], [ 'attributes'=>[ 'Date_Calendaire'=>['type'=>Form::INPUT_WIDGET, 'widgetClass'=>'\kartik\widgets\DatePicker', 'options' => [ 'pluginOptions' => [ 'todayHighlight' => true, 'format' => 'yyyy-mm-dd', 'multidate' => true, 'multidateSeparator' => ' ; ', ], ], 'hint'=>'Select Date', ], ] ], [ 'attributes'=>[ 'actions'=>[ 'type'=>Form::INPUT_RAW, 'value'=> '<div>' . Html::resetButton('Reset', ['class'=>'btn btn-default']) . ' ' . Html::submitButton('Create', ['class' => 'btn btn-primary']) . '</div>' ], ], ], ] ]); ?> <?php ActiveForm::end();?> </div> Anyone can help me ? Thank you =) Edited September 5, 2016 by tsatsar Link to comment Share on other sites More sharing options...
Brent Knigge Posted September 6, 2016 Share Posted September 6, 2016 Hello, I think this link to the documentation for loadMultiple might help. http://www.yiiframework.com/doc-2.0/yii-base-model.html#loadMultiple()-detail Maybe add the formName and see if that helps. if (FeuilleDeJourResponsable::loadMultiple($array_feuille_de_jour_responsable,Yii::$app->request->post(), 'FeuilleDeJourResponsable') ) Also check the Post data to see what the form name is, and ensure that it matches with what you're expecting. Hope this helps, Brent. Link to comment Share on other sites More sharing options...
tsatsar Posted September 9, 2016 Author Share Posted September 9, 2016 Thank you for your answer ! I try but it doesn't work, the same error appear. I don't know how to check the post data :/ I search an other solution but I find nothing that work =( Link to comment Share on other sites More sharing options...
Brent Knigge Posted September 13, 2016 Share Posted September 13, 2016 Have a look at Charles Proxy. I use this and it helps an awful lot, especially with AJAX stuff. You'll see everything going back and forth between your browser and server. The purpose of using this with your problem is to identify the POST variables and form names that are sent from your browser etc. Link to comment Share on other sites More sharing options...
tsatsar Posted October 28, 2016 Author Share Posted October 28, 2016 Thank you for your answer. I didn't find a solution so I'm passed to other way. Link to comment Share on other sites More sharing options...
Recommended Posts