Jump to content
Larry Ullman's Book Forums

Form With Dropdown List From Other Multiple Model Yii2


tsatsar
 Share

Recommended Posts

Hello,

 

I want a form that create a "Feuille de jour" based on dropdownlist from other models.

 

In fact, I have 4 tables that are linked by id : the table "Feuille de jour" have the id from the tables "poste FDJ", "categorie FDJ" and "personnel".

 

When I create a "Feuille de jour", I want to be able to select (in a dropdown list) the name of a "poste FDJ", a "categorie FDJ" and a "personnel" and when I submit the form, it save the id of each name.

 

I know it's possible to save multiple model in one form, I supposed that I can start on this but it doesn't work. 

Can someone help me? 

There are my create function controller (that had worked with a "simple" form) and my view :

 public function actionCreate()
    {	
		$model = new FeuilleDeJourResponsable();
		$meteo_prevision = new MeteoPrevision();
		$poste_fdj = new PosteFdj();
		$categorie_fdj = new CategorieFdj();
		$personnel = new Personnel();

		if ($model->load(Yii::$app->request->post()) && $meteo_prevision->load(Yii::$app->request->post()) && $poste_fdj->load(Yii::$app->request->post()) && $categorie_fdj->load(Yii::$app->request->post()) && $personnel->load(Yii::$app->request->post()) && FeuilleDeJourResponsable::validateMultiple([$model, $meteo_prevision, $poste_fdj, $categorie_fdj, $personnel])) 
		{
			
			$poste_fdj->save(false); 
			$categorie_fdj->save(false);
			$personnel->save(false); 
			
			$model->ID_Poste_FDJ = $poste_fdj->ID_Poste_FDJ; // no need for validation rule on user_id as you set it yourself
			$model->ID_Categorie = $categorie_fdj->ID_Categorie; // no need for validation rule on user_id as you set it yourself
			$model->Code_Personnel = $personnel->Code_Personnel; // no need for validation rule on user_id as you set it yourself
		
			$model->save(false); // skip validation as model is already validated
			
			$meteo_prevision->Date_Calendaire = $model->Date_Calendaire; // no need for validation rule on user_id as you set it yourself
			$meteo_prevision->save(false); 
			
			return $this->redirect(['feuille_de_jour_responsable/view', 'Date_Calendaire' => $model->Date_Calendaire]);
		} else {
			return $this->render('create', [
				'feuille_de_jour_responsable' => $model,
				'meteo_prevision' => $meteo_prevision,
				'poste_fdj' => $poste_fdj,
				'categorie_fdj' => $categorie_fdj,
				'personnel' => $personnel,
			]);
		}
    }
	<?php
	
	
	$reqNomPoste = 'SELECT Nom_Poste_FDJ,ID_Poste_FDJ FROM poste_fdj';
	$nomPoste = PosteFdj::findBySql($reqNomPoste)
	->asArray()
	->all();
	
	/*$IDPoste = array();
	$i = 0;
	
	foreach ($nomPoste["ID_Poste_FDJ"] as $ID)
	{
		$i++;
		$IDPoste = $ID;
	}*/
	//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'=>PosteFdj::find()->select(['Nom_Poste_FDJ', 'ID_Poste_FDJ'])->indexBy('ID_Poste_FDJ')->column(), 'hint'=>'Choisir Poste'],
					//::findBySql('SELECT Nom_Poste_FDJ,ID_Poste_FDJ FROM poste_fdj'), 'hint'=>'Choisir Poste']
					'Code_Personnel'=>['type'=>Form::INPUT_DROPDOWN_LIST, 'items'=>Personnel::find()->select(['Nom_Personnel', 'Code_Personnel'])->indexBy('Code_Personnel')->column(),'hint'=>'Select Personnel'],
					'ID_Categorie'=>['type'=>Form::INPUT_DROPDOWN_LIST, 'items'=>CategorieFdj::find()->select(['Nom', 'ID_Categorie'])->indexBy('ID_Categorie')->column(), 'hint'=>'Choisir Categorie'],
				]
			],
			[
				'attributes'=>[
				//'Date_Calendaire'=>['type'=>Form::INPUT_TEXT, 'options'=>['placeholder'=>'Enter username...']],
				//'Date_Calendaire'=>['type'=>Form::INPUT_WIDGET, 'widgetClass'=>'\kartik\widgets\DatePicker', 'hint'=>'Enter birthday (mm/dd/yyyy)'],
					'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'=>[    // embed raw HTML content
						'type'=>Form::INPUT_RAW, 
						'value'=>  '<div>' . 
						Html::resetButton('Reset', ['class'=>'btn btn-default']) . ' ' .
						Html::submitButton('Submit', ['class'=>'btn btn-primary']) .
						'</div>'
					]
				],
			],
		]
	]);
	
	?>
	<?php ActiveForm::end();?>

Thank you ! =)

Link to comment
Share on other sites

*Sorry for multiple post, I can't edit*

 

I try again with the function create from yii (I don't know why, I try before post :/ )

 

My funcion create :

public function actionCreate()
{
        $feuille_de_jour_responsable = new FeuilleDeJourResponsable();

        if ($feuille_de_jour_responsable->load(Yii::$app->request->post()) && $feuille_de_jour_responsable->save()) {
            return $this->redirect(['view', 'Date_Calendaire' => $feuille_de_jour_responsable->Date_Calendaire, 'ID_Poste_FDJ' => $feuille_de_jour_responsable->ID_Poste_FDJ, 'ID_Categorie' => $feuille_de_jour_responsable->ID_Categorie, 'Code_Personnel' => $feuille_de_jour_responsable->Code_Personnel]);
        } else {
            return $this->render('create', [
                'feuille_de_jour_responsable' => $feuille_de_jour_responsable,
            ]);
        }
}

I try to save multiple "Feuille de jour" in same time but it didn't work. 

There is my fuction create now : 
 

    public function actionCreate()
    {
       /* $feuille_de_jour_responsable = new FeuilleDeJourResponsable();

        if ($feuille_de_jour_responsable->load(Yii::$app->request->post()) && $feuille_de_jour_responsable->save()) {
            return $this->redirect(['view', 'Date_Calendaire' => $feuille_de_jour_responsable->Date_Calendaire, 'ID_Poste_FDJ' => $feuille_de_jour_responsable->ID_Poste_FDJ, 'ID_Categorie' => $feuille_de_jour_responsable->ID_Categorie, 'Code_Personnel' => $feuille_de_jour_responsable->Code_Personnel]);
        } else {
            return $this->render('create', [
                'feuille_de_jour_responsable' => $feuille_de_jour_responsable,
            ]);
        }*/
			
		$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(),"FeuilleDeJourResponsable"))
		{
			foreach ($array_feuille_de_jour_responsable as $feuille)
			{
				$feuille->save(false);
			}
			return $this->redirect('index');
			//return $this->redirect(['view', 'Date_Calendaire' => $feuille_de_jour_responsable->Date_Calendaire, 'ID_Poste_FDJ' => $feuille_de_jour_responsable->ID_Poste_FDJ, 'ID_Categorie' => $feuille_de_jour_responsable->ID_Categorie, 'Code_Personnel' => $feuille_de_jour_responsable->Code_Personnel]);
		} else {
			return $this->render('create', [
				'feuille_de_jour_responsable' => $array_feuille_de_jour_responsable,
			]);
		}
}

With it, I have an error : "

Invalid Configuration – yii\base\InvalidConfigException Either the 'formName' has to be set or a valid 'model' property must be set extending from '\yii\base\Model'."

 

 

Someone have an idea? 

Edited by tsatsar
Link to comment
Share on other sites

  • 2 weeks later...

Sorry again for double post but I can't edit.

 

I try something else : 

 

<?php $form = ActiveForm::begin(['feuille-de-jour-responsable/create']);
    
    echo FormGrid::widget([
        'model'=>$feuille_de_jour_responsable,
        'form'=>$form,
        'autoGenerateColumns'=>true,
        'rows'=>[
            [
                'attributes'=>[
                    'ID_Categorie'=>['type'=>Form::INPUT_DROPDOWN_LIST, 'items'=>CategorieFdj::find()->select(['Nom', 'ID_Categorie'])->indexBy('ID_Categorie')->column(),'id'=>'cat-id','hint'=>'Choisir Categorie'],
                    'ID_Poste_FDJ'=>['type'=>Form::INPUT_DROPDOWN_LIST, 'items'=>PosteFdj::find()->select(['Nom_Poste_FDJ', 'ID_Poste_FDJ'])->indexBy('ID_Poste_FDJ')->column(), 'hint'=>'Choisir Poste'],
                    /*'ID_Poste_FDJ'=>['type'=>Form::INPUT_WIDGET, 'widgetClass'=>'\kartik\depdrop\DepDrop', 'options'=>['id'=>'poste-id'],
                            'pluginOptions'=>[
                                'depends'=>['cat-id'],
                                'placeholder'=>'Select...',
                                //'url'=>Url::to(['file:///C:/wamp64/www/test/yii/basic/controllers/PosteFdjController.php'])
                                //'url'=>Url::to(['http://127.0.0.1:81/test/yii/basic/web/index.php?r=poste-fdj%2Findex'])
                                'url'=> Url::to(['/site/feuille-de-jour-responsable'])
                            ]
                    ],*/
                    'Code_Personnel'=>['type'=>Form::INPUT_DROPDOWN_LIST, 'items'=>Personnel::find()->select(['Nom_Personnel', 'Code_Personnel'])->indexBy('Code_Personnel')->column(),'hint'=>'Select Personnel'],
                ]
            ],
            [
                'attributes'=>[
                //'Date_Calendaire'=>['type'=>Form::INPUT_TEXT, 'options'=>['placeholder'=>'Enter username...']],
                //'Date_Calendaire'=>['type'=>Form::INPUT_WIDGET, 'widgetClass'=>'\kartik\widgets\DatePicker', 'hint'=>'Enter birthday (mm/dd/yyyy)'],
                    'Date_Calendaire'=>['string',Yii::$app->request->post('string'),'type'=>Form::INPUT_WIDGET, 'widgetClass'=>'\kartik\widgets\DatePicker',
                        'options' => [
                            'pluginOptions' => [
                                'todayHighlight' => true,
                                'format' => 'yyyy-mm-dd',
                                'multidate' => true,
                                'multidateSeparator' => ' ; ',
                            ],
                        ],
                        'hint'=>'Select Date',
                    ],
                ]
            ],
            [
                'attributes'=>[
                    'actions'=>[ // embed raw HTML content
                        'type'=>Form::INPUT_RAW,
                        'value'=> '<div>' .
                        Html::resetButton('Reset', ['class'=>'btn btn-default']) . ' ' .
                        Html::submitButton('Create', ['class'=>'btn btn-lg btn-primary', 'name' => 'create-button']) .
                        '</div>'
                    ]
                ],
            ],
        ]
    ]);
    
    ?>
    <?php ActiveForm::end();?> 

And my create function : 

   public function actionCreate()
    {
        $feuille_de_jour_responsable = new FeuilleDeJourResponsable();

        if ($feuille_de_jour_responsable->load(Yii::$app->request->post()) && $feuille_de_jour_responsable->save()) {
            return $this->redirect(['view', 'Date_Calendaire' => $feuille_de_jour_responsable->Date_Calendaire, 'ID_Poste_FDJ' => $feuille_de_jour_responsable->ID_Poste_FDJ, 'ID_Categorie' => $feuille_de_jour_responsable->ID_Categorie, 'Code_Personnel' => $feuille_de_jour_responsable->Code_Personnel]);
        } else {
            return $this->render('create', [
                'feuille_de_jour_responsable' => $feuille_de_jour_responsable,
            ]);
        }
} 

And I remove the rules.

Now I have this error : 

SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: '2016-09-28 ; 2016-09-15 ; 2016-09-13' for column 'Date_Calendaire' at row 1
The SQL being executed was: INSERT INTO `feuille_de_jour_responsable` (`ID_Categorie`, `ID_Poste_FDJ`, `Code_Personnel`, `Date_Calendaire`) VALUES (1, 1, 4901, '2016-09-28 ; 2016-09-15 ; 2016-09-13')

 

I need to cut the string '2016-09-28 ; 2016-09-15 ; 2016-09-13' for separate the different dates.

But I don't know how it can work to save multiple...

Link to comment
Share on other sites

*Sorry again, can't edit*

 

I think I'm progressing.

I'm using Pjax :

My form : 
 

<?php  Pjax::begin(); ?>
       
        <?php $form = ActiveForm::begin();
       
        echo FormGrid::widget([
                'model'=>$feuille_de_jour_responsable,
                'form'=>$form,
                'autoGenerateColumns'=>true,
                'rows'=>[
                        [
                                'attributes'=>[
                                        'ID_Categorie'=>['type'=>Form::INPUT_DROPDOWN_LIST, 'items'=>CategorieFdj::find()->select(['Nom', 'ID_Categorie'])->indexBy('ID_Categorie')->column(),'id'=>'cat-id','hint'=>'Choisir Categorie'],
                                        'ID_Poste_FDJ'=>['type'=>Form::INPUT_DROPDOWN_LIST, 'items'=>PosteFdj::find()->select(['Nom_Poste_FDJ', 'ID_Poste_FDJ'])->indexBy('ID_Poste_FDJ')->column(), 'hint'=>'Choisir Poste'],
                                        /*'ID_Poste_FDJ'=>['type'=>Form::INPUT_WIDGET, 'widgetClass'=>'\kartik\depdrop\DepDrop', 'options'=>['id'=>'poste-id'],
                                                        'pluginOptions'=>[
                                                                'depends'=>['cat-id'],
                                                                'placeholder'=>'Select...',
                                                                //'url'=>Url::to(['file:///C:/wamp64/www/test/yii/basic/controllers/PosteFdjController.php'])
                                                                //'url'=>Url::to(['http://127.0.0.1:81/test/yii/basic/web/index.php?r=poste-fdj%2Findex'])
                                                                'url'=> Url::to(['/site/feuille-de-jour-responsable'])
                                                        ]
                                        ],*/
                                        'Code_Personnel'=>['type'=>Form::INPUT_DROPDOWN_LIST, 'items'=>Personnel::find()->select(['Nom_Personnel', 'Code_Personnel'])->indexBy('Code_Personnel')->column(),'hint'=>'Select Personnel'],
                                ]
                        ],
                        [
                                'attributes'=>[
                                //'Date_Calendaire'=>['type'=>Form::INPUT_TEXT, 'options'=>['placeholder'=>'Enter username...']],
                                //'Date_Calendaire'=>['type'=>Form::INPUT_WIDGET, 'widgetClass'=>'\kartik\widgets\DatePicker', 'hint'=>'Enter birthday (mm/dd/yyyy)'],
                                        'Date_Calendaire'=>['string',Yii::$app->request->post('string'),'type'=>Form::INPUT_WIDGET, 'widgetClass'=>'\kartik\widgets\DatePicker',
                                                'options' => [
                                                        'pluginOptions' => [
                                                                'todayHighlight' => true,
                                                                'format' => 'yyyy-mm-dd',
                                                                'multidate' => true,
                                                                'multidateSeparator' => ';',
                                                        ],
                                                ],
                                                'hint'=>'Select Date',
                                        ],
                                ]
                        ],
                        [
                                'attributes'=>[      
                                        'actions'=>[    // embed raw HTML content
                                                'type'=>Form::INPUT_RAW,
                                                'value'=>  '<div>' .
                                                Html::resetButton('Reset', ['class'=>'btn btn-default']) . ' ' .
                                                Html::submitButton('Create', ['class'=>'btn btn-lg btn-primary', 'name' => 'create-button']) .
                                                '</div>'
                                        ]
                                ],
                        ],
                ]
        ]);
       
        ?>
        <?php ActiveForm::end();?>
        <h3><?= $stringf ?></h3>
        <h3><?= var_dump($stringcut) ?></h3>
        <?php Pjax::end(); ?>

My function : 

   

 public function actionCreate()
    {
                $feuille_de_jour_responsable = new FeuilleDeJourResponsable();
                //pour pjax
                $stringf = Yii::$app->request->post('string');
                $stringcut = explode(";", $stringf);
               
                return $this->render('create', [
                'feuille_de_jour_responsable' => $feuille_de_jour_responsable,
                        'stringcut' => $stringcut,
                        'stringf'=>$stringf,
                ]);

                /*$feuille_de_jour_responsable = new FeuilleDeJourResponsable();

        if ($feuille_de_jour_responsable->load(Yii::$app->request->post()) && $feuille_de_jour_responsable->save()) {
            return $this->redirect(['view', 'Date_Calendaire' => $feuille_de_jour_responsable->Date_Calendaire, 'ID_Poste_FDJ' => $feuille_de_jour_responsable->ID_Poste_FDJ, 'ID_Categorie' => $feuille_de_jour_responsable->ID_Categorie, 'Code_Personnel' => $feuille_de_jour_responsable->Code_Personnel]);
        } else {
            return $this->render('create', [
                'feuille_de_jour_responsable' => $feuille_de_jour_responsable,
            ]);
        }*/
}

I would test if I can cut this string and after that, I could save for each part of the string a new "feuilleDeJour" 

But it seems it do nothing. When I click on my button, the page is refreshed but nothing else...
var_dump stay "Array(1) { [0]=> string(0) "" }"

Link to comment
Share on other sites

  • 1 month later...
 Share

×
×
  • Create New...