@@ -125,18 +125,16 @@ public function run()
125125 if (empty ($ form ->fieldConfig ['labelOptions ' ])) {
126126 $ form ->fieldConfig ['labelOptions ' ] = ['class ' => 'control-label ' ];
127127 }
128+ $ keys = array_keys ($ this ->models );
128129 $ form ->fieldConfig ['template ' ] = str_replace ('{input} ' , $ this ->inputTemplate , $ form ->fieldConfig ['template ' ]);
129130 $ button = Html::a (
130131 Html::tag ('span ' , '' , [
131132 'class ' => 'glyphicon glyphicon-plus ' ,
132133 ]),
133- array_merge (( array ) $ this ->urlAdd ) ,
134+ $ this ->urlAdd ,
134135 $ this ->buttonOptions
135136 );
136- $ field = $ form ->field ($ this ->models [0 ], "[0] {$ this ->attribute }" , $ this ->fieldOptions );
137- $ field = call_user_func_array ([$ field , $ this ->inputMethod ], $ this ->inputMethodArgs );
138- echo str_replace ('{button} ' , $ button , $ field );
139- 137+ echo $ this ->field ($ form , $ this ->models [$ keys [0 ]], "[ {$ keys [0 ]}] {$ this ->attribute }" , $ button );
140138 if (!$ this ->labelEach ) {
141139 $ form ->fieldConfig ['template ' ] = str_replace (
142140 '{label} ' ,
@@ -151,24 +149,35 @@ public function run()
151149 $ form ->fieldConfig ['template ' ]
152150 );
153151 }
154- for ($ i = 1 ; $ i != count ($ this -> models ); $ i ++) {
152+ for ($ i = 1 ; $ i != count ($ keys ); $ i ++) {
155153 $ button = Html::a (
156154 Html::tag ('span ' , '' , [
157155 'class ' => 'glyphicon glyphicon-minus ' ,
158156 ]),
159157 array_merge ((array )$ this ->urlRemove , ['id ' => $ this ->models [$ i ]->{$ this ->primaryKey }]),
160158 $ this ->buttonOptions
161159 );
162- $ field = $ form ->field ($ this ->models [$ i ], "[ {$ i }] {$ this ->attribute }" , $ this ->fieldOptions );
163- $ field = call_user_func_array ([$ field , $ this ->inputMethod ], $ this ->inputMethodArgs );
164- echo str_replace ('{button} ' , $ button , $ field );
165- }
166- 160+ echo $ this ->field ($ form , $ this ->models [$ keys [$ i ]], "[ {$ keys [$ i ]}] {$ this ->attribute }" , $ button );
161+ }
167162 $ this ->form ->attributes = $ form ->attributes ;
168- 169163 Pjax::end ();
170164 }
171165
166+ /**
167+ * Render field of \yii\widgets\ActiveForm.
168+ * @param \yii\widgets\ActiveForm $form
169+ * @param \yii\base\Model $model The data model.
170+ * @param string $attribute Attribute name.
171+ * @param string $button Action button of field.
172+ * @return string
173+ */
174+ protected function field ($ form , $ model , $ attribute , $ button )
175+ {
176+ $ field = $ form ->field ($ model , $ attribute , $ this ->fieldOptions );
177+ $ field = call_user_func_array ([$ field , $ this ->inputMethod ], $ this ->inputMethodArgs );
178+ return str_replace ('{button} ' , $ button , $ field );
179+ }
180+ 172181 /**
173182 * @return boolean whether this widget is associated with a data model.
174183 */
@@ -186,4 +195,4 @@ protected function hasModel()
186195 return true ;
187196 }
188197
189- }
198+ }
0 commit comments