Skip to content

Commit 2510db7

Browse files
author
Eugene Tupikov
committed
#175 option generalErrorPosition to enable displaying of general error message
1 parent a584dbe commit 2510db7

File tree

7 files changed

+53
-10
lines changed

7 files changed

+53
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Yii2 multiple input change log
66
- #215 collect all js script that has to be evaluate when add new row (not only from " on ready" section)
77
- #198 introduce the option `theme` to disable all bootstrap css classes
88
- #197 explicitly set tabindex for all inputs
9+
- #175 option `generalErrorPosition` to enable displaying of general error message
910

1011
2.16.0
1112
======

examples/models/ExampleModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function rules()
9696
['title', 'string', 'min' => 5],
9797
['emails', 'validateEmails'],
9898
['phones', 'validatePhones'],
99-
['schedule', 'validateSchedule'],
99+
['schedule', 'validateSchedule', 'skipOnEmpty' => false],
100100
['questions', 'validateQuestions']
101101
];
102102
}

examples/views/multiple-input.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
'validateOnBlur' => false,
2222
]);?>
2323

24+
2425
<h3>Single column</h3>
2526
<?php
2627
echo $form->field($model, 'emails')->widget(MultipleInput::className(), [
@@ -52,6 +53,7 @@
5253
'max' => 4,
5354
'sortable' => true,
5455
'allowEmptyList' => true,
56+
'generalErrorPosition' => 'top',
5557
'rowOptions' => function($model) {
5658
$options = [];
5759

src/MultipleInput.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,14 @@ class MultipleInput extends InputWidget
3737

3838
const ICONS_SOURCE_GLYPHICONS = 'glyphicons';
3939
const ICONS_SOURCE_FONTAWESOME = 'fa';
40+
41+
const GENERAL_ERROR_POS_TOP = 'top';
42+
const GENERAL_ERROR_POS_BOTTOM = 'bottom';
4043

4144
/**
4245
* @var ActiveRecordInterface[]|array[] input data
4346
*/
44-
public $data = null;
47+
public $data;
4548

4649
/**
4750
* @var array columns configuration
@@ -205,6 +208,17 @@ class MultipleInput extends InputWidget
205208
*/
206209
public $theme = self::THEME_BS;
207210

211+
/**
212+
* @var string|null set a position of generic error message (when you set an error for generic field instead of particular input)
213+
* If set to null no generic error message will be shown.
214+
*/
215+
public $generalErrorPosition;
216+
217+
/**
218+
* @var bool
219+
*/
220+
private $showGeneralError = false;
221+
208222
/**
209223
* Initialization.
210224
*
@@ -216,6 +230,16 @@ public function init()
216230
throw new InvalidConfigException('Property "form" must be an instance of yii\widgets\ActiveForm');
217231
}
218232

233+
$this->showGeneralError = $this->generalErrorPosition !== null && !$this->isEmbedded && $this->field;
234+
235+
if ($this->showGeneralError) {
236+
if ($this->generalErrorPosition === self::GENERAL_ERROR_POS_BOTTOM) {
237+
$this->field->template = "{input}\n{error}";
238+
} else {
239+
$this->field->template = "{error}\n{input}";
240+
}
241+
}
242+
219243
$this->guessColumns();
220244
$this->initData();
221245

@@ -276,9 +300,10 @@ protected function guessColumns()
276300
public function run()
277301
{
278302
$content = '';
279-
if ($this->hasModel() && $this->isEmbedded === false) {
303+
if ($this->isEmbedded === false && $this->hasModel()) {
280304
$content .= Html::hiddenInput(Html::getInputName($this->model, $this->attribute), null);
281305
}
306+
282307
$content .= $this->createRenderer()->render();
283308

284309
return $content;
@@ -339,6 +364,12 @@ private function createRenderer()
339364
'theme' => $this->theme
340365
];
341366

367+
if ($this->showGeneralError) {
368+
$config['jsExtraSettings'] = [
369+
'showGeneralError' => true
370+
];
371+
}
372+
342373
if ($this->removeButtonOptions !== null) {
343374
$config['removeButtonOptions'] = $this->removeButtonOptions;
344375
}

src/assets/src/js/jquery.multipleInput.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@
9999
/**
100100
* default prefix of a widget's placeholder
101101
*/
102-
indexPlaceholder: 'multiple_index'
102+
indexPlaceholder: 'multiple_index',
103+
104+
showGeneralError: false
103105
};
104106

105107
var isActiveFormEnabled = false;
@@ -162,7 +164,9 @@
162164
}
163165
});
164166

165-
form.yiiActiveForm('remove', inputId);
167+
if (!settings.showGeneralError) {
168+
form.yiiActiveForm('remove', inputId);
169+
}
166170
}
167171

168172
// append default options to option from settings

src/assets/src/js/jquery.multipleInput.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/renderers/BaseRenderer.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ abstract class BaseRenderer extends BaseObject implements RendererInterface
107107
public $columnClass;
108108

109109
/**
110-
* @var string position of add button. By default button is rendered in the row.
110+
* @var array|string position of add button. By default button is rendered in the row.
111111
*/
112112
public $addButtonPosition = self::POS_ROW;
113113

@@ -178,6 +178,11 @@ abstract class BaseRenderer extends BaseObject implements RendererInterface
178178
*/
179179
public $theme = self::THEME_BS;
180180

181+
/**
182+
* @var array
183+
*/
184+
public $jsExtraSettings = [];
185+
181186
/**
182187
* @inheritdoc
183188
*/
@@ -392,7 +397,7 @@ public function render()
392397
}
393398
}
394399

395-
$options = Json::encode([
400+
$options = Json::encode(array_merge([
396401
'id' => $this->id,
397402
'inputId' => $this->context->options['id'],
398403
'template' => $template,
@@ -402,7 +407,7 @@ public function render()
402407
'min' => $this->min,
403408
'attributes' => $this->prepareJsAttributes(),
404409
'indexPlaceholder' => $this->getIndexPlaceholder()
405-
]);
410+
], $this->jsExtraSettings));
406411

407412
$js = "jQuery('#{$this->id}').multipleInput($options);";
408413
$view->registerJs($js);
@@ -519,7 +524,7 @@ protected function prepareJsAttributes()
519524
if (isset($attributeOptions['name']) && $attributeOptions['name'] === $column->name) {
520525
$attributes[$inputID] = ArrayHelper::merge($attributeOptions, $column->attributeOptions);
521526
} else {
522-
array_push($this->form->attributes, $attributeOptions);
527+
$this->form->attributes[] = $attributeOptions;
523528
}
524529
} else {
525530
$attributes[$inputID] = $column->attributeOptions;

0 commit comments

Comments
 (0)