Jump to content
Larry Ullman's Book Forums

Save Multiple Instance With Datepicker And Based On Different Models


tsatsar
 Share

Recommended Posts

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 by tsatsar
Link to comment
Share on other sites

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

  • 1 month later...
 Share

×
×
  • Create New...