Dynamic fields question


Yii 2.0.1, I have a registration form and I want to add dynamic fieldset that should be saved in separate table.

So user loads the form, he sees 1 fieldset and [Add more] button, when the button is clicked, another fieldset should be added below with Javascript, fields validated and when the form is submitted it should insert the fields in main table and in additional table for the fieldset.

How can I implement it?

Create a function in your controller that would add a fieldset. Make ajax call from the [Add more] button to that function and append the data to the form.

In the actionSave you will have to save the models separately. The main one and the other ones. I don’t know about the validation of dynamically added fields.

Let me know if you need sample code or something is unclear. Or post some parts of your code.


Sounds like a good plan.

I’m so new to Yii2 that I’m still trying to figure out how to do it.

I’ve already generated the _form view for the fieldset, so trying to figure out how to add it to the form on load and on [Add more] button click.

Could you please guide me which functions should be used to append data to form on button click?

I think I will not have problems saving things to database.


I’ll illustrate with an example.

Model name is Example, function name in controller will be actionTest

in your _form.php have a button, counter, to know how many fields you have added, and a div in which the data will be placed.


echo Html::input('button','btn_add','Add More',[

		'onclick'=>'$.post( "' . Yii::$app->urlManager->createUrl(['example/test', 'i'=>'']).'"+$("#counter").val(),

				function( data ){

					var val = $("#counter").val();

					$( "#counter" ).val( parseInt(val) + 1 );

					$( "div#newlyaddedfields" ).append( data );


		',//onclick end



<?php echo Html::hiddenInput('counter',0,['id'=>'palcnt',]); ?>

<div id="newlyaddedfields"></div>

in your ExampleController.php

public function actionTest($i)


		$mod = new Example();

		echo Html::activeTextInput($mod,"[$i]testattribute");


If you want to add the fields on load it’s probably fine to just add them straight up in the _form.

Oh my god, this is genious!! Thank you, Will paste my final code of the form tomorrow!!

It doesn’t want to validate the fields in this way. I think will have to write additional validation rules for dynamic things.

How can I implement [Remove fieldset] ?

Yeah, I’m kind of stuck at validating dynamically added fields too…

The remove would probably have to be done with some jquery magic too.

I’ve moved fields out of controller…

now I’m doing



echo Html::input(‘button’,‘btn_add’,‘Add More’,[

            'onclick'=&gt;'&#036;.post( &quot;' . Yii::&#036;app-&gt;urlManager-&gt;createUrl(['employeequalifications/create', 'i'=&gt;'']).'&quot;+&#036;(&quot;#counter&quot;).val(),

                            function( data ){

                                    var val = &#036;(&quot;#counter&quot;).val();

                                    &#036;( &quot;#counter&quot; ).val( parseInt(val) + 1 );

                                    &#036;( &quot;div#newlyaddedfields&quot; ).append( data );

									&#036;( &quot;div#newlyaddedfields&quot; ).wrap( &quot;&lt;div class=&#092;'new&#092;'&gt;&lt;/div&gt;&quot; );


            ',//onclick end

]); ?>

Controller (employeequalifications/create):

public function actionCreate($i)


	&#036;model = new EmployeeQualifications();

	return &#036;this-&gt;renderPartial('create',[




EmployeeQualifications View create:

<div class="employee-qualifications-create">

&lt;h1&gt;&lt;?= Html::encode(&#036;this-&gt;title) ?&gt;&lt;/h1&gt;

&lt;?= &#036;this-&gt;render('_form', [

    'model' =&gt; &#036;model,

]) ?&gt;


and the view _form is:

<div class="employee-qualifications-form">

&lt;?php &#036;form = ActiveForm::begin(); ?&gt;

&lt;?= &#036;form-&gt;field(&#036;model, 'employee_qualification_owner_id')-&gt;textInput() ?&gt;

&lt;?= &#036;form-&gt;field(&#036;model, 'employee_qualification_position')-&gt;textInput() ?&gt;

&lt;?= &#036;form-&gt;field(&#036;model, 'employee_qualification_start_date')-&gt;textInput(['maxlength' =&gt; 255]) ?&gt;

&lt;?= &#036;form-&gt;field(&#036;model, 'employee_qualification_end_date')-&gt;textInput(['maxlength' =&gt; 255]) ?&gt;

&lt;?= &#036;form-&gt;field(&#036;model, 'employee_qualification_reason')-&gt;textInput(['maxlength' =&gt; 255]) ?&gt;

&lt;?= &#036;form-&gt;field(&#036;model, 'employee_qualification_organisation')-&gt;textInput(['maxlength' =&gt; 255]) ?&gt;

&lt;?= &#036;form-&gt;field(&#036;model, 'employee_qualification_town')-&gt;textInput(['maxlength' =&gt; 255]) ?&gt;

&lt;?= &#036;form-&gt;field(&#036;model, 'employee_qualification_country')-&gt;textInput() ?&gt;

&lt;div class=&quot;form-group&quot;&gt;

    &lt;? //Html::submitButton(&#036;model-&gt;isNewRecord ? 'Add' : 'Update', ['class' =&gt; &#036;model-&gt;isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?&gt;


&lt;a href=&quot;javascript:void();&quot; onclick=&quot;&#036;(this).closest(&quot;.employee-qualifications-form&quot;).remove();&quot;&gt;Remove&lt;/a&gt;

&lt;?php ActiveForm::end(); ?&gt;


want to get rid of the ActiveForm in _form since I don’t need it.

then will move on validation of dynamically added fields.

I have added

<?= yii\jui\DatePicker::widget([‘name’ => ‘employee_qualification_start_date’, ‘clientOptions’ => [‘defaultDate’ => ‘2014-01-01’]]) ?>

in _form, but the calendar is not popping up… hmmm…

found this…

trying to implement it, I think it should work as I need :)

Looks like it does, and also supports Datepicker widgets and first record validation.

still problems with datepicker on cloned

https://github.com/wbraganca/yii2-dynamicform is highly recommended. I managed to do what I needed

Nice, I’ll try that tomorrow. I’m guessing the validation works too.

I changed the concept so I don’t need it anymore, had too many problems with it :)