Skip to content

Commit 9cf86d3

Browse files
committed
#81 fixed output of errors in case of non-ajax validation
1 parent b87b778 commit 9cf86d3

File tree

4 files changed

+85
-31
lines changed

4 files changed

+85
-31
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.18 in development
55
---------------------
66

7+
- #81 fixed output of errors in case of non-ajax validation
8+
79
1.2.17
810
------
911

src/MultipleInputColumn.php

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,24 @@ public function getElementName($index, $withPrefix = true)
4949
if (is_null($index)) {
5050
$index = '{multiple_index}';
5151
}
52-
$elementName = count($this->renderer->columns) > 1
53-
? '[' . $index . '][' . $this->name . ']'
54-
: '[' . $this->name . '][' . $index . ']';
52+
53+
$elementName = $this->isRendererHasOneColumn()
54+
? '[' . $this->name . '][' . $index . ']'
55+
: '[' . $index . '][' . $this->name . ']';
56+
5557
$prefix = $withPrefix ? $this->getInputNamePrefix() : '';
58+
5659
return $prefix . $elementName;
5760
}
5861

62+
/**
63+
* @return bool
64+
*/
65+
private function isRendererHasOneColumn()
66+
{
67+
return count($this->renderer->columns) === 1;
68+
}
69+
5970
/**
6071
* Return prefix for name of input.
6172
*
@@ -65,11 +76,13 @@ protected function getInputNamePrefix()
6576
{
6677
$model = $this->context->model;
6778
if ($model instanceof Model) {
68-
if (empty($this->renderer->columns) || (count($this->renderer->columns) == 1 && $this->hasModelAttribute($this->name))) {
79+
if (empty($this->renderer->columns) || ($this->isRendererHasOneColumn() && $this->hasModelAttribute($this->name))) {
6980
return $model->formName();
7081
}
82+
7183
return Html::getInputName($this->context->model, $this->context->attribute);
7284
}
85+
7386
return $this->context->name;
7487
}
7588

@@ -86,9 +99,27 @@ protected function hasModelAttribute($name)
8699
}
87100
}
88101

102+
/**
103+
* @param int|string|null $index
104+
* @return null|string
105+
*/
89106
public function getFirstError($index)
90107
{
91-
$attribute = $this->context->attribute . $this->getElementName($index, false);
92-
return $this->context->model->getFirstError($attribute);
108+
if ($index === null) {
109+
return null;
110+
}
111+
112+
if ($this->isRendererHasOneColumn()) {
113+
$attribute = $this->name . '[' . $index . ']';
114+
} else {
115+
$attribute = $this->context->attribute . $this->getElementName($index, false);
116+
}
117+
118+
$model = $this->context->model;
119+
if ($model instanceof Model) {
120+
return $model->getFirstError($attribute);
121+
}
122+
123+
return null;
93124
}
94125
}

src/components/BaseColumn.php

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ private function normalize($name) {
227227
/**
228228
* Renders the input.
229229
*
230-
* @param string $name name of the input
231-
* @param array $options the input options
230+
* @param string $name the name of the input
231+
* @param array $options the HTML options of input
232232
* @return string
233233
*/
234234
public function renderInput($name, $options)
@@ -288,9 +288,9 @@ private function prepareItems($items)
288288
/**
289289
* Renders list box.
290290
*
291-
* @param $name
292-
* @param $value
293-
* @param $options
291+
* @param string $name the name of input
292+
* @param mixed $value the value of input
293+
* @param array $options the HTMl options of input
294294
* @return string
295295
*/
296296
protected function renderListBox($name, $value, $options)
@@ -302,9 +302,9 @@ protected function renderListBox($name, $value, $options)
302302
/**
303303
* Renders hidden input.
304304
*
305-
* @param $name
306-
* @param $value
307-
* @param $options
305+
* @param string $name the name of input
306+
* @param mixed $value the value of input
307+
* @param array $options the HTMl options of input
308308
* @return string
309309
*/
310310
protected function renderHiddenInput($name, $value, $options)
@@ -315,9 +315,9 @@ protected function renderHiddenInput($name, $value, $options)
315315
/**
316316
* Renders radio button.
317317
*
318-
* @param $name
319-
* @param $value
320-
* @param $options
318+
* @param string $name the name of input
319+
* @param mixed $value the value of input
320+
* @param array $options the HTMl options of input
321321
* @return string
322322
*/
323323
protected function renderRadio($name, $value, $options)
@@ -335,9 +335,9 @@ protected function renderRadio($name, $value, $options)
335335
/**
336336
* Renders radio button list.
337337
*
338-
* @param $name
339-
* @param $value
340-
* @param $options
338+
* @param string $name the name of input
339+
* @param mixed $value the value of input
340+
* @param array $options the HTMl options of input
341341
* @return string
342342
*/
343343
protected function renderRadioList($name, $value, $options)
@@ -360,9 +360,9 @@ protected function renderRadioList($name, $value, $options)
360360
/**
361361
* Renders checkbox.
362362
*
363-
* @param $name
364-
* @param $value
365-
* @param $options
363+
* @param string $name the name of input
364+
* @param mixed $value the value of input
365+
* @param array $options the HTMl options of input
366366
* @return string
367367
*/
368368
protected function renderCheckbox($name, $value, $options)
@@ -380,9 +380,9 @@ protected function renderCheckbox($name, $value, $options)
380380
/**
381381
* Renders checkbox list.
382382
*
383-
* @param $name
384-
* @param $value
385-
* @param $options
383+
* @param string $name the name of input
384+
* @param mixed $value the value of input
385+
* @param array $options the HTMl options of input
386386
* @return string
387387
*/
388388
protected function renderCheckboxList($name, $value, $options)
@@ -402,6 +402,17 @@ protected function renderCheckboxList($name, $value, $options)
402402
return Html::tag('div', $input, ['class' => 'checkbox-list']);
403403
}
404404

405+
/**
406+
* @param $name
407+
* @param $value
408+
* @param $options
409+
* @return string
410+
*/
411+
protected function renderStatic($name, $value, $options)
412+
{
413+
return Html::tag('p', $value, ['class' => 'form-control-static']);
414+
}
415+
405416
/**
406417
* Renders an input.
407418
*
@@ -415,9 +426,7 @@ protected function renderDefault($name, $value, $options)
415426
{
416427
$type = $this->type;
417428

418-
if ($type == self::TYPE_STATIC) {
419-
$input = Html::tag('p', $value, ['class' => 'form-control-static']);
420-
} elseif (method_exists('yii\helpers\Html', $type)) {
429+
if (method_exists('yii\helpers\Html', $type)) {
421430
Html::addCssClass($options, 'form-control');
422431
$input = Html::$type($name, $value, $options);
423432
} elseif (class_exists($type) && method_exists($type, 'widget')) {

src/renderers/TableRenderer.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,14 @@ private function hasHeader()
7777
if ($this->min === 0) {
7878
return true;
7979
}
80+
8081
foreach ($this->columns as $column) {
8182
/* @var $column BaseColumn */
8283
if ($column->title) {
8384
return true;
8485
}
8586
}
87+
8688
return false;
8789
}
8890

@@ -98,6 +100,7 @@ private function renderHeaderCell($column)
98100
}
99101
$options = $column->headerOptions;
100102
Html::addCssClass($options, 'list-cell__' . $column->name);
103+
101104
return Html::tag('th', $column->title, $options);
102105
}
103106

@@ -130,6 +133,7 @@ protected function renderBody()
130133
$rows[] = $this->renderRowContent($i);
131134
}
132135
}
136+
133137
return Html::tag('tbody', implode("\n", $rows));
134138
}
135139

@@ -190,6 +194,7 @@ protected function prepareRowOptions($index, $item)
190194
}
191195

192196
Html::addCssClass($options, 'multiple-input-list__item');
197+
193198
return $options;
194199
}
195200

@@ -213,10 +218,14 @@ public function renderCellContent($column, $index)
213218
}
214219

215220
$hasError = false;
216-
if ($column->enableError) {
221+
222+
if ($index !== null) {
217223
$error = $column->getFirstError($index);
218224
$hasError = !empty($error);
219-
$input .= "\n" . $column->renderError($error);
225+
226+
if ($column->enableError) {
227+
$input .= "\n" . $column->renderError($error);
228+
}
220229
}
221230

222231
$wrapperOptions = [
@@ -226,6 +235,7 @@ public function renderCellContent($column, $index)
226235
if ($hasError) {
227236
Html::addCssClass($wrapperOptions, 'has-error');
228237
}
238+
229239
$input = Html::tag('div', $input, $wrapperOptions);
230240

231241
return Html::tag('td', $input, [
@@ -270,6 +280,7 @@ private function renderAddButton()
270280
'class' => 'btn multiple-input-list__btn js-input-plus',
271281
];
272282
Html::addCssClass($options, $this->addButtonOptions['class']);
283+
273284
return Html::tag('div', $this->addButtonOptions['label'], $options);
274285
}
275286

@@ -285,6 +296,7 @@ private function renderRemoveButton()
285296
'class' => 'btn multiple-input-list__btn js-input-remove',
286297
];
287298
Html::addCssClass($options, $this->removeButtonOptions['class']);
299+
288300
return Html::tag('div', $this->removeButtonOptions['label'], $options);
289301
}
290302

0 commit comments

Comments
 (0)