Skip to content

Commit 9dd5bbc

Browse files
committed
#79 Rendering of errors for embedded MultipleInput + update changelog
1 parent aee79fd commit 9dd5bbc

File tree

5 files changed

+42
-4
lines changed

5 files changed

+42
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Yii2 multiple input change log
44
1.2.20 in development
55
---------------------
66

7+
- #79 Added support for embedded MultipleInput widget (unclead, execut)
8+
79
1.2.19
810
------
911

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Contents:
2222
- [Ajax loading of a widget](#ajax-loading)
2323
- [Use of a widget's placeholder](#using-placeholder)
2424
- [Custom index of the row](#custom-index)
25+
- [Embedded MultipleInput widget](#enbedded)
2526
- [Javascript Events](#javascript-events)
2627
- [Renderers](#renderers)
2728

@@ -447,6 +448,10 @@ as in the example below:
447448
```
448449

449450

451+
### Embedded MultipleInput widget <a id="embedded"></a>
452+
453+
TBD
454+
450455
## JavaScript events
451456
This widget has following events:
452457
- `afterInit`: triggered after initialization

examples/views/multiple-input.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
?>
1515

1616
<?php $form = ActiveForm::begin([
17-
'enableAjaxValidation' => false,
17+
'enableAjaxValidation' => true,
1818
'enableClientValidation' => false,
1919
'validateOnChange' => false,
2020
'validateOnSubmit' => true,

src/MultipleInputColumn.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function getElementName($index, $withPrefix = true)
5353
$elementName = $this->isRendererHasOneColumn()
5454
? '[' . $this->name . '][' . $index . ']'
5555
: '[' . $index . '][' . $this->name . ']';
56-
56+
5757
$prefix = $withPrefix ? $this->getInputNamePrefix() : '';
5858

5959
return $prefix . $elementName;
@@ -122,4 +122,27 @@ public function getFirstError($index)
122122

123123
return null;
124124
}
125+
126+
/**
127+
* @inheritdoc
128+
*/
129+
protected function renderWidget($type, $name, $value, $options)
130+
{
131+
// Extend options in case of rendering embedded MultipleInput
132+
// We have to pass to the widget original model and attribute to be able get first error from model
133+
// for embedded widget.
134+
if ($type === MultipleInput::class && strpos($name, $this->renderer->getIndexPlaceholder()) === false) {
135+
$model = $this->context->model;
136+
137+
$search = sprintf('%s[%s]', $model->formName(), $this->context->attribute);
138+
$replace = $this->context->attribute;
139+
140+
$attribute = str_replace($search, $replace, $name);
141+
142+
$options['model'] = $model;
143+
$options['attribute'] = $attribute;
144+
}
145+
146+
return parent::renderWidget($type, $name, $value, $options);
147+
}
125148
}

src/renderers/TableRenderer.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function renderHeader()
6060
}
6161

6262
if ($this->limit === null || ($this->limit >= 1 && $this->limit !== $this->min)) {
63-
$button = $this->min === 0 || $this->addButtonPosition === self::POS_HEADER ? $this->renderAddButton() : '';
63+
$button = $this->min === 0 || $this->isAddButtonPositionHeader() ? $this->renderAddButton() : '';
6464

6565
$cells[] = Html::tag('th', $button, [
6666
'class' => 'list-cell__button'
@@ -70,14 +70,22 @@ public function renderHeader()
7070
return Html::tag('thead', Html::tag('tr', implode("\n", $cells)));
7171
}
7272

73+
/**
74+
* @return bool
75+
*/
76+
private function isAddButtonPositionHeader()
77+
{
78+
return $this->addButtonPosition === self::POS_HEADER;
79+
}
80+
7381
/**
7482
* Check that at least one column has a header.
7583
*
7684
* @return bool
7785
*/
7886
private function hasHeader()
7987
{
80-
if ($this->min === 0) {
88+
if ($this->min === 0 || $this->isAddButtonPositionHeader()) {
8189
return true;
8290
}
8391

0 commit comments

Comments
 (0)