Skip to content

Commit be9194b

Browse files
author
Eugene Tupikov
committed
added ability to allow an empty list (or set min property to 0) for TabularInput
1 parent 12fcc73 commit be9194b

File tree

4 files changed

+40
-16
lines changed

4 files changed

+40
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Yii2 multiple input change log
33

44
2.13.0
55
======
6+
- #152 added ability to allow an empty list (or set `min` property to 0) for TabularInput
67

78
2.12.0
89
======

examples/views/tabular-input.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
<?= TabularInput::widget([
2222
'models' => $models,
23+
'modelClass' => Item::class,
24+
'min' => 0,
2325
'attributeOptions' => [
2426
'enableAjaxValidation' => true,
2527
'enableClientValidation' => false,

src/TabularColumn.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
*/
1818
class TabularColumn extends BaseColumn
1919
{
20+
/**
21+
* @var TabularInput
22+
*/
23+
public $context;
24+
2025
/**
2126
* Returns element's name.
2227
*
@@ -63,16 +68,10 @@ protected function ensureModel($model)
6368
*/
6469
public function setModel($model)
6570
{
66-
$currentModel = $this->getModel();
67-
68-
// If model is null and current model is not empty it means that widget renders a template
69-
// In this case we have to unset all model attributes
70-
if ($model === null && $currentModel !== null) {
71-
foreach ($currentModel->attributes() as $attribute) {
72-
$currentModel->$attribute = null;
73-
}
74-
} else {
75-
parent::setModel($model);
71+
if ($model === null) {
72+
$model = \Yii::createObject(['class' => $this->context->modelClass]);
7673
}
74+
75+
parent::setModel($model);
7776
}
7877
}

src/TabularInput.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class TabularInput extends Widget
6868
/**
6969
* @var Model[]|ActiveRecordInterface[]
7070
*/
71-
public $models;
71+
public $models = [];
7272

7373
/**
7474
* @var string|array position of add button.
@@ -122,25 +122,47 @@ class TabularInput extends Widget
122122
*/
123123
public $enableError = false;
124124

125+
/**
126+
* @var string a class of model which is used to render the widget.
127+
* You have to specify this property in case you set `min` property to 0 (when you want to allow an empty list)
128+
* @since 2.13
129+
*/
130+
public $modelClass;
131+
125132
/**
126133
* Initialization.
127134
*
128135
* @throws \yii\base\InvalidConfigException
129136
*/
130137
public function init()
131138
{
132-
if (empty($this->models)) {
133-
throw new InvalidConfigException('You must specify "models"');
139+
if (empty($this->models) && !$this->modelClass) {
140+
throw new InvalidConfigException('You must at least specify "models" or "modelClass"');
134141
}
135142

136143
if ($this->form !== null && !$this->form instanceof ActiveForm) {
137144
throw new InvalidConfigException('Property "form" must be an instance of yii\widgets\ActiveForm');
138145
}
139146

140-
foreach ($this->models as $model) {
141-
if (!$model instanceof Model) {
142-
throw new InvalidConfigException('Model has to be an instance of yii\base\Model');
147+
if (!is_array($this->models)) {
148+
throw new InvalidConfigException('Property "models" must be an array');
149+
}
150+
151+
if ($this->models) {
152+
$modelClasses = [];
153+
foreach ($this->models as $model) {
154+
if (!$model instanceof Model) {
155+
throw new InvalidConfigException('Model has to be an instance of yii\base\Model');
156+
}
157+
158+
$modelClasses[get_class($model)] = true;
159+
}
160+
161+
if (count($modelClasses) > 1) {
162+
throw new InvalidConfigException("You cannot use models of different classes");
143163
}
164+
165+
$this->modelClass = key($modelClasses);
144166
}
145167

146168
parent::init();

0 commit comments

Comments
 (0)