Skip to content

Commit 8ac7f9b

Browse files
author
Eugene Tupikov
committed
Fix display inline errors in case using form without ajax validation
1 parent 4bed574 commit 8ac7f9b

File tree

5 files changed

+57
-19
lines changed

5 files changed

+57
-19
lines changed

docs/multiple_columns.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ use yii\helpers\Html;
8686
[
8787
'name' => 'priority',
8888
'title' => 'Priority',
89+
'enableError' => true,
8990
'options' => [
9091
'class' => 'input-priority'
9192
]

examples/actions/MultipleInputAction.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ public function run()
2828
return $result;
2929
}
3030

31-
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
32-
31+
if ($model->load(Yii::$app->request->post())) {
32+
if (!$model->validate()) {
33+
Yii::error('Validation errors: ' . print_r($model->getErrors(), true));
34+
}
3335
}
36+
37+
3438
return $this->controller->render('@unclead-examples/views/example.php', ['model' => $model]);
3539
}
3640
}

examples/models/ExampleModel.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ public function init()
4343
$this->schedule = [
4444
[
4545
'day' => '27.02.2015',
46-
'user_id' => 1,
46+
'user_id' => 31,
4747
'priority' => 1
4848
],
4949
[
5050
'day' => '27.02.2015',
51-
'user_id' => 2,
51+
'user_id' => 33,
5252
'priority' => 2
5353
],
5454
];
@@ -139,10 +139,14 @@ public function validateSchedule($attribute)
139139

140140
foreach($this->$attribute as $index => $row) {
141141
$error = null;
142-
$requiredValidator->validate($row['priority'], $error);
143-
if (!empty($error)) {
144-
$key = $attribute . '[' . $index . '][priority]';
145-
$this->addError($key, $error);
142+
foreach (['user_id', 'priority'] as $name) {
143+
$error = null;
144+
$value = isset($row[$name]) ? $row[$name] : null;
145+
$requiredValidator->validate($value, $error);
146+
if (!empty($error)) {
147+
$key = $attribute . '[' . $index . '][' . $name . ']';
148+
$this->addError($key, $error);
149+
}
146150
}
147151
}
148152
}

src/MultipleInput.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,8 @@ private function renderRowContent($index = null, $data = null)
253253
private function collectJsTemplates()
254254
{
255255
if (is_array($this->getView()->js) && array_key_exists(View::POS_READY, $this->getView()->js)) {
256-
$this->jsTemplates = [];
257-
foreach ($this->getView()->js[View::POS_READY] as $key => $js) {
256+
$this->jsTemplates = [];
257+
foreach ($this->getView()->js[View::POS_READY] as $key => $js) {
258258
if (preg_match('/^.*' . $this->options['id'] . '-{multiple-index}.*$/', $js) === 1) {
259259
$this->jsTemplates[] = $js;
260260
unset($this->getView()->js[View::POS_READY][$key]);
@@ -301,17 +301,20 @@ private function renderActionColumn($index = null)
301301
* Returns element's name.
302302
*
303303
* @param string $name the name of cell element
304-
* @param int|null $index
304+
* @param int|null $index current row index
305+
* @param bool $withPrefix whether to add prefix.
305306
* @return string
306307
*/
307-
public function getElementName($name, $index)
308+
public function getElementName($name, $index, $withPrefix = true)
308309
{
309310
if (is_null($index)) {
310311
$index = '{multiple-index}';
311312
}
312-
return $this->getInputNamePrefix($name) . (
313-
count($this->columns) > 1 ? '[' . $index . '][' . $name . ']' : '[' . $name . '][' . $index . ']'
314-
);
313+
$elementName = count($this->columns) > 1
314+
? '[' . $index . '][' . $name . ']'
315+
: '[' . $name . '][' . $index . ']';
316+
$prefix = $withPrefix ? $this->getInputNamePrefix($name) : '';
317+
return $prefix . $elementName;
315318
}
316319

317320
/**

src/MultipleInputColumn.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ public function init()
9292
throw new InvalidConfigException("The 'name' option is required.");
9393
}
9494

95+
if ($this->enableError && empty($this->widget->model)) {
96+
throw new InvalidConfigException('Property "enableError" available only when model is defined.');
97+
}
98+
9599
if (is_null($this->type)) {
96100
$this->type = self::TYPE_TEXT_INPUT;
97101
}
@@ -207,16 +211,38 @@ public function renderCellContent($value, $index)
207211
return $input;
208212
}
209213

214+
$hasError = false;
210215
if ($this->enableError) {
211-
$input .= "\n" . Html::tag('div', '', $this->errorOptions);
216+
$attribute = $this->widget->attribute . $this->widget->getElementName($this->name, $index, false);
217+
$error = $this->widget->model->getFirstError($attribute);
218+
$hasError = !empty($error);
219+
$input .= "\n" . $this->renderError($error);
212220
}
213221

214-
$input = Html::tag('div', $input, [
215-
'class' => 'form-group field-' . $options['id'],
216-
]);
222+
$wrapperOptions = [
223+
'class' => 'form-group field-' . $options['id']
224+
];
225+
226+
if ($hasError) {
227+
Html::addCssClass($wrapperOptions, 'has-error');
228+
}
229+
$input = Html::tag('div', $input, $wrapperOptions);
217230

218231
return Html::tag('td', $input, [
219232
'class' => 'list-cell__' . $this->name,
220233
]);
221234
}
235+
236+
/**
237+
* @param string $error
238+
* @return string
239+
*/
240+
private function renderError($error)
241+
{
242+
$options = $this->errorOptions;
243+
$tag = isset($options['tag']) ? $options['tag'] : 'div';
244+
$encode = !isset($options['encode']) || $options['encode'] !== false;
245+
unset($options['tag'], $options['encode']);
246+
return Html::tag($tag, $encode ? Html::encode($error) : $error, $options);
247+
}
222248
}

0 commit comments

Comments
 (0)