Save multiple instance with datepicker and based on different models

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 =)

No one? =(

I know nothing about yii2, but the error suggests that either of these entries is causing the problem:

                'model'=>$feuille_de_jour_responsable,
                'form'=>$form,

So, have you checked that they both contain what you expect them to contain? I can see where you create $form but not where you create the other variable, presumably something somewhere in the rest of the code is doing that.

Thank you for your answer !
For arrive at this page (where I have the error) I click on a “create button” and I think that the “$feuille_de_jour_responsable” is created at this moment.
But I don’t know how I can check if it contain what I expect…

I have an another form that works :

<?php

use yii\helpers\Html;

use kartik\widgets\ActiveForm;
use kartik\builder\Form;
use kartik\builder\FormGrid;


/* @var $this yii\web\View */
/* @var $model app\models\CategorieFdj */

$this->title = 'Create Categorie Fdj';
$this->params['breadcrumbs'][] = ['label' => 'Categorie Fdjs', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="categorie-fdj-create">

  <!-- <h1><?//= Html::encode($this->title) ?></h1>
<?php 
$form = ActiveForm::begin(['type'=>ActiveForm::TYPE_VERTICAL]);
echo FormGrid::widget([
'model'=>$model,
'form'=>$form,
'autoGenerateColumns'=>true,
'rows'=>[
	[
		'contentBefore'=>'<legend class="text-info">Creer une catégorie</legend>',
		'attributes'=>[       // 2 column layout
			'Nom'=>['type'=>Form::INPUT_TEXT, 'options'=>['placeholder'=>'Entrer nom']],
			'Couleur'=>['type'=>Form::INPUT_WIDGET, 'widgetClass'=>'\kartik\widgets\ColorInput', 'hint'=>'Choose your color'],
		]
	],
	[
		'attributes'=>[    
			'actions'=>[    // embed raw HTML content
				'type'=>Form::INPUT_RAW, 
				'value'=>  '<div style="text-align: right; margin-top: 20px">' . 
					Html::submitButton('Submit', ['class'=>'btn btn-primary']) . 
					'</div>'
			],
		],
	],
]
]);
ActiveForm::end();
?>

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.