Skip to content

Commit 8f370af

Browse files
committed
Adjustments for correct work with AR relations
1 parent 07ca184 commit 8f370af

File tree

5 files changed

+48
-28
lines changed

5 files changed

+48
-28
lines changed

src/MultipleInput.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ class MultipleInput extends InputWidget
108108
*/
109109
public $rendererClass;
110110

111+
/**
112+
* @var bool whether the widget is embedded or not.
113+
* @internal this property is used for internal purposes. Do not use it in your code.
114+
*/
115+
public $isEmbedded;
116+
111117
/**
112118
* Initialization.
113119
*
@@ -117,6 +123,7 @@ public function init()
117123
{
118124
$this->guessColumns();
119125
$this->initData();
126+
120127
parent::init();
121128
}
122129

src/MultipleInputColumn.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,20 @@ public function getElementName($index, $withPrefix = true)
4949
if (is_null($index)) {
5050
$index = '{' . $this->renderer->getIndexPlaceholder() . '}';
5151
}
52-
52+
5353
$elementName = $this->isRendererHasOneColumn()
5454
? '[' . $this->name . '][' . $index . ']'
5555
: '[' . $index . '][' . $this->name . ']';
5656

57-
$prefix = $withPrefix ? $this->getInputNamePrefix() : '';
58-
57+
if (!$withPrefix) {
58+
return $elementName;
59+
}
60+
61+
$prefix = $this->getInputNamePrefix();
62+
if ($this->context->isEmbedded && strpos($prefix, $this->context->name) === false) {
63+
$prefix = $this->context->name;
64+
}
65+
5966
return $prefix . $elementName;
6067
}
6168

@@ -147,6 +154,12 @@ protected function renderWidget($type, $name, $value, $options)
147154

148155
$options['model'] = $model;
149156
$options['attribute'] = $attribute;
157+
158+
// Remember current name and mark the widget as embedded to prevent
159+
// generation of wrong prefix in case when column is associated with AR relation
160+
// @see https://github.com/unclead/yii2-multiple-input/issues/92
161+
$options['name'] = $name;
162+
$options['isEmbedded'] = true;
150163
}
151164

152165
return parent::renderWidget($type, $name, $value, $options);

src/assets/src/js/jquery.multipleInput.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@
138138
// wait for initialization of ActiveForm a second
139139
// If after a second system could not detect ActiveForm it means
140140
// that widget is used without ActiveForm and we should just complete initialization of the widget
141-
if (i > 10) {
141+
if (form.length === 0 || i > 10) {
142142
$wrapper.data('multipleInput').currentIndex = getCurrentIndex($wrapper);
143143
isActiveFormEnabled = false;
144144

src/assets/src/js/jquery.multipleInput.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/renderers/BaseRenderer.php

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ abstract class BaseRenderer extends Object implements RendererInterface
111111
* @var string
112112
*/
113113
private $indexPlaceholder;
114-
114+
115115
/**
116116
* @inheritdoc
117117
*/
@@ -248,10 +248,29 @@ protected function registerAssets()
248248
{
249249
$view = $this->context->getView();
250250
MultipleInputAsset::register($view);
251-
252-
$jsBefore = $this->collectJsTemplates();
251+
252+
$view = $this->context->getView();
253+
254+
$jsBefore= [];
255+
if (is_array($view->js) && array_key_exists(View::POS_READY, $view->js)) {
256+
foreach ($view->js[View::POS_READY] as $key => $js) {
257+
$jsBefore[$key] = $js;
258+
}
259+
}
260+
253261
$template = $this->prepareTemplate();
254-
$jsTemplates = $this->collectJsTemplates($jsBefore);
262+
263+
$jsTemplates = [];
264+
if (is_array($view->js) && array_key_exists(View::POS_READY, $view->js)) {
265+
foreach ($view->js[View::POS_READY] as $key => $js) {
266+
if (array_key_exists($key, $jsBefore)) {
267+
continue;
268+
}
269+
270+
$jsTemplates[$key] = $js;
271+
unset($view->js[View::POS_READY][$key]);
272+
}
273+
}
255274

256275
$options = Json::encode([
257276
'id' => $this->id,
@@ -272,25 +291,6 @@ protected function registerAssets()
272291
*/
273292
abstract protected function prepareTemplate();
274293

275-
276-
protected function collectJsTemplates($except = [])
277-
{
278-
$view = $this->context->getView();
279-
$output = [];
280-
if (is_array($view->js) && array_key_exists(View::POS_READY, $view->js)) {
281-
foreach ($view->js[View::POS_READY] as $key => $js) {
282-
if (array_key_exists($key, $except)) {
283-
continue;
284-
}
285-
if (preg_match('/^[^{]+{' . $this->getIndexPlaceholder() . '}.*$/m', $js) === 1) {
286-
$output[$key] = $js;
287-
unset($view->js[View::POS_READY][$key]);
288-
}
289-
}
290-
}
291-
return $output;
292-
}
293-
294294
/**
295295
* @return mixed
296296
*/

0 commit comments

Comments
 (0)