Skip to content

Commit edff08e

Browse files
committed
Respect "defaultValue" if it is set and current value is empty
1 parent 7f3d8e9 commit edff08e

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Yii2 multiple input change log
22
==============================
33

4+
1.4.1
5+
=====
6+
7+
- #99: Respect "defaultValue" if it is set and current value is empty (unclead)
8+
49
1.4.0
510
-----
611

examples/views/tabular-input.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
]) ?>
2020

2121
<?= TabularInput::widget([
22-
'models' => $models,
22+
'models' => [],
2323
'attributeOptions' => [
2424
'enableAjaxValidation' => true,
2525
'enableClientValidation' => false,
@@ -36,6 +36,7 @@
3636
'name' => 'title',
3737
'title' => 'Title',
3838
'type' => TabularColumn::TYPE_TEXT_INPUT,
39+
'defaultValue' => 'Test',
3940
'enableError' => true
4041
],
4142
[

src/components/BaseColumn.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use yii\helpers\ArrayHelper;
1717
use yii\helpers\Html;
1818
use yii\helpers\Inflector;
19+
use unclead\widgets\renderers\BaseRenderer;
1920

2021
/**
2122
* Class BaseColumn.
@@ -179,6 +180,7 @@ protected function prepareValue()
179180
if ($this->value instanceof \Closure) {
180181
$value = call_user_func($this->value, $data);
181182
} else {
183+
$value = null;
182184
if ($data instanceof ActiveRecordInterface ) {
183185
$value = $data->getAttribute($this->name);
184186
} elseif ($data instanceof Model) {
@@ -187,13 +189,20 @@ protected function prepareValue()
187189
$value = ArrayHelper::getValue($data, $this->name, null);
188190
} elseif(is_string($data) || is_numeric($data)) {
189191
$value = $data;
190-
}else {
192+
}
193+
194+
if ($this->isEmpty($value) && $this->defaultValue !== null) {
191195
$value = $this->defaultValue;
192196
}
193197
}
194198
return $value;
195199
}
196200

201+
protected function isEmpty($value)
202+
{
203+
return $value === null || $value === [] || $value === '';
204+
}
205+
197206
/**
198207
* Returns element id.
199208
*

0 commit comments

Comments
 (0)