Skip to content

Commit 422f359

Browse files
committed
Bug #62: Fixed incorrect behavior is case when min is equal to limit
1 parent 4206c37 commit 422f359

File tree

3 files changed

+45
-34
lines changed

3 files changed

+45
-34
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ Yii2 multiple input change log
55
---------------------
66

77
- Bug #61: Fixed a rendering of remove button
8-
- Bug #64 Radio/checkbox lists doesn't work correctly
8+
- Bug #62: Incorrect behavior is case when `min` is equal to `limit`
9+
- Bug #64: Radio/checkbox lists doesn't work correctly
910

1011
1.2.10
1112
------

src/components/BaseRenderer.php

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public function init()
130130

131131
private function prepareColumnClass()
132132
{
133-
if (empty($this->columnClass)) {
133+
if (!$this->columnClass) {
134134
throw new InvalidConfigException('You must specify "columnClass"');
135135
}
136136

@@ -142,15 +142,15 @@ private function prepareColumnClass()
142142
private function prepareMinOption()
143143
{
144144
// Set value of min option based on value of allowEmptyList for BC
145-
if (is_null($this->min)) {
145+
if ($this->min === null) {
146146
$this->min = $this->allowEmptyList ? 0 : 1;
147147
} else {
148148
if ($this->min < 0) {
149149
throw new InvalidConfigException('Option "min" cannot be less 0');
150150
}
151151

152152
// Allow empty list in case when minimum number of rows equal 0.
153-
if ($this->min == 0 && !$this->allowEmptyList) {
153+
if ($this->min === 0 && !$this->allowEmptyList) {
154154
$this->allowEmptyList = true;
155155
}
156156

@@ -163,7 +163,7 @@ private function prepareMinOption()
163163

164164
private function prepareLimit()
165165
{
166-
if (is_null($this->limit)) {
166+
if ($this->limit === null) {
167167
$this->limit = 999;
168168
}
169169

@@ -172,33 +172,35 @@ private function prepareLimit()
172172
}
173173

174174
// Maximum number of rows cannot be less then minimum number.
175-
if (!is_null($this->limit) && $this->limit < $this->min) {
175+
if ($this->limit !== null && $this->limit < $this->min) {
176176
$this->limit = $this->min;
177177
}
178178
}
179179

180180
private function prepareButtonsOptions()
181181
{
182-
if (!isset($this->removeButtonOptions['class'])) {
182+
if (!array_key_exists('class', $this->removeButtonOptions)) {
183183
$this->removeButtonOptions['class'] = 'btn btn-danger';
184184
}
185185

186-
if (!isset($this->removeButtonOptions['label'])) {
186+
if (!array_key_exists('label', $this->removeButtonOptions)) {
187187
$this->removeButtonOptions['label'] = Html::tag('i', null, ['class' => 'glyphicon glyphicon-remove']);
188188
}
189189

190-
if (!isset($this->addButtonOptions['class'])) {
190+
if (!array_key_exists('class', $this->addButtonOptions)) {
191191
$this->addButtonOptions['class'] = 'btn btn-default';
192192
}
193193

194-
if (!isset($this->addButtonOptions['label'])) {
194+
if (!array_key_exists('label', $this->addButtonOptions)) {
195195
$this->addButtonOptions['label'] = Html::tag('i', null, ['class' => 'glyphicon glyphicon-plus']);
196196
}
197197
}
198198

199199

200200
/**
201201
* Creates column objects and initializes them.
202+
*
203+
* @throws \yii\base\InvalidConfigException
202204
*/
203205
protected function initColumns()
204206
{
@@ -211,8 +213,8 @@ protected function initColumns()
211213
if ($this->context instanceof MultipleInput) {
212214
$definition['widget'] = $this->context;
213215
}
214-
$column = Yii::createObject($definition);
215-
$this->columns[$i] = $column;
216+
217+
$this->columns[$i] = Yii::createObject($definition);
216218
}
217219
}
218220

@@ -233,6 +235,7 @@ abstract protected function internalRender();
233235
/**
234236
* Register script.
235237
*
238+
* @throws \yii\base\InvalidParamException
236239
*/
237240
protected function registerClientScript()
238241
{
@@ -243,16 +246,14 @@ protected function registerClientScript()
243246
$template = $this->prepareTemplate();
244247
$jsTemplates = $this->collectJsTemplates($jsBefore);
245248

246-
$options = Json::encode(
247-
[
248-
'id' => $this->id,
249-
'template' => $template,
250-
'jsTemplates' => $jsTemplates,
251-
'limit' => $this->limit,
252-
'min' => $this->min,
253-
'attributeOptions' => $this->attributeOptions,
254-
]
255-
);
249+
$options = Json::encode([
250+
'id' => $this->id,
251+
'template' => $template,
252+
'jsTemplates' => $jsTemplates,
253+
'limit' => $this->limit,
254+
'min' => $this->min,
255+
'attributeOptions' => $this->attributeOptions,
256+
]);
256257

257258
$js = "jQuery('#{$this->id}').multipleInput($options);";
258259
$view->registerJs($js);

src/renderers/TableRenderer.php

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use yii\base\InvalidConfigException;
1212
use yii\db\ActiveRecord;
13+
use yii\helpers\ArrayHelper;
1314
use yii\helpers\Html;
1415
use unclead\widgets\components\BaseRenderer;
1516
use unclead\widgets\components\BaseColumn;
@@ -55,8 +56,9 @@ public function renderHeader()
5556
$cells[] = $this->renderHeaderCell($column);
5657
}
5758

58-
if (is_null($this->limit) || ($this->limit >= 1 && $this->limit != $this->min)) {
59-
$button = $this->min == 0 || $this->addButtonPosition == self::POS_HEADER ? $this->renderAddButton() : '';
59+
if ($this->limit === null || ($this->limit >= 1 && $this->limit !== $this->min)) {
60+
$button = $this->min === 0 || $this->addButtonPosition === self::POS_HEADER ? $this->renderAddButton() : '';
61+
6062
$cells[] = Html::tag('th', $button, [
6163
'class' => 'list-cell__button'
6264
]);
@@ -72,12 +74,12 @@ public function renderHeader()
7274
*/
7375
private function hasHeader()
7476
{
75-
if ($this->min == 0) {
77+
if ($this->min === 0) {
7678
return true;
7779
}
7880
foreach ($this->columns as $column) {
7981
/* @var $column BaseColumn */
80-
if (!empty($column->title)) {
82+
if ($column->title) {
8183
return true;
8284
}
8385
}
@@ -103,14 +105,21 @@ private function renderHeaderCell($column)
103105
* Renders the body.
104106
*
105107
* @return string
108+
* @throws \yii\base\InvalidConfigException
109+
* @throws \yii\base\InvalidParamException
106110
*/
107111
protected function renderBody()
108112
{
109113
$rows = [];
110114

111-
if (!empty($this->data)) {
112-
foreach ($this->data as $index => $item) {
113-
$rows[] = $this->renderRowContent($index, $item);
115+
if ($this->data) {
116+
$cnt = count($this->data);
117+
if ($this->min === $this->limit && $cnt < $this->limit) {
118+
$cnt = $this->limit;
119+
}
120+
for ($i = 0; $i < $cnt; $i++) {
121+
$item = ArrayHelper::getValue($this->data, $i, null);
122+
$rows[] = $this->renderRowContent($i, $item);
114123
}
115124
} elseif ($this->min > 0) {
116125
for ($i = 0; $i < $this->min; $i++) {
@@ -146,7 +155,7 @@ private function renderRowContent($index = null, $item = null)
146155
$cells[] = $this->renderActionColumn($index);
147156
}
148157

149-
if (!empty($hiddenInputs)) {
158+
if ($hiddenInputs) {
150159
$hiddenInputs = implode("\n", $hiddenInputs);
151160
$cells[0] = preg_replace('/^(<td[^>]+>)(.*)(<\/td>)$/s', '${1}' . $hiddenInputs . '$2$3', $cells[0]);
152161
}
@@ -233,15 +242,15 @@ private function renderActionColumn($index = null)
233242

234243
private function getActionButton($index)
235244
{
236-
if (is_null($index) || $this->min == 0) {
245+
if ($index === null || $this->min === 0) {
237246
return $this->renderRemoveButton();
238247
}
239248

240-
$index += 1;
249+
$index++;
241250
if ($index < $this->min) {
242251
return '';
243-
} elseif ($index == $this->min) {
244-
return $this->addButtonPosition == self::POS_ROW ? $this->renderAddButton() : '';
252+
} elseif ($index === $this->min) {
253+
return $this->addButtonPosition === self::POS_ROW ? $this->renderAddButton() : '';
245254
} else {
246255
return $this->renderRemoveButton();
247256
}

0 commit comments

Comments
 (0)