Skip to content

Commit ffbc49b

Browse files
author
Eugene Tupikov
committed
Fixes
1 parent 3c7b734 commit ffbc49b

File tree

5 files changed

+39
-16
lines changed

5 files changed

+39
-16
lines changed

MultipleInput.php

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,11 @@ public function run()
8484
echo Html::beginTag('table', [
8585
'class' => 'multiple-input-list table table-condensed'
8686
]);
87+
8788
if ($this->hasHeader()) {
8889
$this->renderHeader();
8990
}
91+
9092
echo Html::beginTag('tbody');
9193
if (!empty($this->data)) {
9294
foreach ($this->data as $index => $data) {
@@ -96,8 +98,10 @@ public function run()
9698
$this->renderRow(0);
9799
}
98100
echo Html::endTag('tbody');
101+
99102
echo Html::endTag('table');
100103
echo Html::endTag('div');
104+
101105
$this->registerClientScript();
102106
}
103107
/**
@@ -131,6 +135,7 @@ private function renderHeader()
131135
echo Html::endTag('tr');
132136
echo Html::endTag('thead');
133137
}
138+
134139
/**
135140
* Check that at least one column has a header.
136141
*
@@ -145,6 +150,7 @@ private function hasHeader()
145150
}
146151
return false;
147152
}
153+
148154
private function getRowTemplate()
149155
{
150156
if (empty($this->template)) {
@@ -180,17 +186,17 @@ private function getRowTemplate()
180186
$options['selectedOption'] = $value;
181187
$this->template .= Html::$type($name, null, $column['items'], $options);
182188
break;
183-
case 'custom':
184-
$this->template .= $value;
185-
break;
186189
default:
187190
if (method_exists('yii\helpers\Html', $type)) {
188191
$this->template .= Html::$type($name, $value, $options);
192+
// TODO https://github.com/unclead/yii2-multiple-input/issues/1
193+
/*
189194
} elseif (class_exists($type) && method_exists($type, 'widget')) {
190195
$this->template .= $type::widget(array_merge($options, [
191196
'name' => $name,
192197
'value' => $value,
193198
]));
199+
*/
194200
} else {
195201
throw new InvalidConfigException("Invalid column type '$type'");
196202
}
@@ -236,13 +242,15 @@ private function renderRow($index, $data = null)
236242
$btnType = $index == 0 ? 'btn-default' : 'btn-danger';
237243
$search = ['{index}', '{btn_action}', '{btn_type}'];
238244
$replace = [$index, $btnAction, $btnType];
245+
239246
foreach ($this->getColumns() as $column) {
240247
if (!array_key_exists('name', $column)) {
241248
throw new InvalidConfigException("The 'name' option is required.");
242249
}
243250
$search[] = '{' . $column['name'] . '_value}';
244251
$replace[] = $this->prepareColumnValue($column, $data);
245252
}
253+
246254
echo str_replace($search, $replace, $this->getRowTemplate());
247255
}
248256

@@ -258,6 +266,8 @@ private function getColumns()
258266
}
259267

260268
/**
269+
* Preparing column's value.
270+
*
261271
* @param $column
262272
* @param $data
263273
* @return mixed
@@ -284,7 +294,9 @@ private function prepareColumnValue($column, $data)
284294
}
285295

286296
/**
287-
* @param $name
297+
* Returns element's name.
298+
*
299+
* @param string $name
288300
* @param string $index
289301
* @return string
290302
*/
@@ -293,25 +305,33 @@ private function getElementName($name, $index = null)
293305
if ($index === null) {
294306
$index = '{index}';
295307
}
296-
return $this->getAttributeName() . (
308+
return $this->getInputNamePrefix($name) . (
297309
count($this->columns) > 1
298310
? '[' . $index . '][' . $name . ']'
299311
: '[' . $name . '][' . $index . ']'
300312
);
301313
}
302314

303315
/**
316+
* Return prefix for name of input.
317+
*
318+
* @param string $name input name
304319
* @return string
305320
*/
306-
private function getAttributeName()
321+
private function getInputNamePrefix($name)
307322
{
308323
if ($this->hasModel()) {
309-
return empty($this->columns) ? $this->model->formName() : Html::getInputName($this->model, $this->attribute);
324+
if (empty($this->columns) || (count($this->columns) == 1 && $this->model->hasProperty($name))) {
325+
return $this->model->formName();
326+
}
327+
return Html::getInputName($this->model, $this->attribute);
310328
}
311329
return $this->name;
312330
}
313331

314332
/**
333+
* Returns element id.
334+
*
315335
* @param $name
316336
* @return mixed
317337
*/
@@ -321,6 +341,8 @@ private function getElementId($name)
321341
}
322342

323343
/**
344+
* Normalization name.
345+
*
324346
* @param $name
325347
* @return mixed
326348
*/

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,6 @@ Each column in a row can has their own type. Widget supports:
124124
- `textarea`
125125
- For more detail look at [Html helper class](http://www.yiiframework.com/doc-2.0/yii-helpers-html.html)
126126

127-
- use type `static` in case when you just want to render text instead of input
128-
129127
##License
130128

131129
**yii2-multiple-input** is released under the BSD 3-Clause License. See the bundled [LICENSE.md](./LICENSE.md) for details.

assets/src/js/jquery.multipleInput.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,14 @@
4444

4545
var intervalID = setInterval(function(){
4646
if (typeof form.data('yiiActiveForm') === 'object') {
47-
$.each(form.yiiActiveForm('find', id), function (key, value) {
48-
if (['id', 'input', 'container'].indexOf(key) == -1) {
49-
wrapper.data('multipleInput').attributeDefaults[key] = value;
50-
}
51-
});
47+
var attribute = form.yiiActiveForm('find', id);
48+
if (attribute !== false) {
49+
$.each(attribute, function (key, value) {
50+
if (['id', 'input', 'container'].indexOf(key) == -1) {
51+
wrapper.data('multipleInput').attributeDefaults[key] = value;
52+
}
53+
});
54+
}
5255
form.yiiActiveForm('remove', id);
5356
wrapper.find('.multiple-input-list').find('input, select, textarea').each(function () {
5457
methods.addAttribute.apply(this);

examples/actions/MultipleInputAction.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public function run()
2424
if ($request->isPost && $request->post('ajax') !== null) {
2525
$model->load(Yii::$app->request->post());
2626
Yii::$app->response->format = Response::FORMAT_JSON;
27-
return ActiveForm::validate($model);
27+
$result = ActiveForm::validate($model);
28+
return $result;
2829
}
2930

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

examples/views/example.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
->label(false);
2525
?>
2626

27-
2827
<h3>Multiple columns</h3>
2928
<?= $form->field($model, 'schedule')->widget(MultipleInput::className(), [
3029
'limit' => 4,

0 commit comments

Comments
 (0)