Multiple same instance with just one field that change

Hi,

I want to make a form that create multiple instance with just one field that change (the date).

In fact, I want to save when works a person.

So, I choose the name and service once and I choose multiple dates.

I try many things but don’t work or don’t seems appropriate.

I need to finish this project as soon as possible…

Someone have an idea to do it?

do you need to save "id categorie" "id poste fdj" "code personnel" and the date every time the date field changes?

I don’t know if I understood your problem, but you could return to the create page every time you submit the form.

maybe something like this:




    public function actionCreate()

    {

        $model = new YourModel();


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

            return $this->render('create', [

                'model' => $model,

            ]);

        } else {

            return $this->render('create', [

                'model' => $model,

            ]);

        }

    }



If you don’t want to lose the data that you put in those fields, you need to do it with ajax instead, for submit the information and save it without reload the page.

see the following link to get more information about how to do a submit using ajax

http://www.yiiframework.com/wiki/772/pjax-on-activeform-and-gridview-yii2/

that example help me a lot, I hope that helps you too.

Thank you for your answer !

I don’t want to return to the page “create” for each instance. I want to save, for each date selected on the datepicker (kartik) the same “id categorie”, “id poste fdj” and “code personnel” that I have selected just once.

I use this 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,

            ]);

        }

}

But it doen’t work with my form :


<?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'],

					'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_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();?>

I don’t know how to do to make it work…

I will test with what you say.

Thank you !

I see. You have to use Ajax. Study the example that I’ve recommended you in my previous response.

I hope you can get it work

I’m studying Pjax, but I don’t understand how it can help me.

I don’t want to update dynamically my form, I want to save for each dates I select.

You think I can do a button like : "for each dates seleted on datepicker widget, save(with the fuction on the controller) the id categorie, id poste fdj and code personnel selected on dropdown list" with Pjax?

like this Form submission ?

And if I can do it, how can I get the dates on datepicker?

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 got an error, before try this :

"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’)"

So, 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) "" }"