Skip to content

Commit 0dd00cf

Browse files
author
Eugene Tupikov
committed
Began writing example
1 parent 341fc56 commit 0dd00cf

File tree

7 files changed

+168
-34
lines changed

7 files changed

+168
-34
lines changed

MultipleInput.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ class MultipleInput extends InputWidget
5252
*/
5353
protected $replacementKeys;
5454

55-
5655
public function init()
5756
{
5857
parent::init();
@@ -184,7 +183,7 @@ private function getRowTemplate()
184183
$this->template .= $value;
185184
break;
186185
default:
187-
if (method_exists('albatross\commons\helpers\Html', $type)) {
186+
if (method_exists('yii\helpers\Html', $type)) {
188187
$this->template .= Html::$type($name, $value, $options);
189188
} elseif (class_exists($type) && method_exists($type, 'widget')) {
190189
$this->template .= $type::widget(array_merge($options, [
@@ -301,7 +300,7 @@ private function normalize($name) {
301300
*/
302301
private function getGroupId()
303302
{
304-
return $this->normalize($this->getName());
303+
return $this->normalize($this->getName()) . static::$counter;
305304
}
306305

307306
/**

README.md

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,4 @@
11
# yii2-multiple-field
22

33

4-
* For example, model as an attribute contacts and you want to add to user ability input more than one
5-
* value.
6-
*
7-
* In this case you have to use this wi
8-
*
9-
* Пример использования виджета в случае, когда используется 1 атрибут, а на выходе надо
10-
* получить массив со значениями сгруппированными по определенному ключу.
11-
* Например, у нас есть атрибут contacts и он может быть разных типов, например email
12-
*
13-
* Пример конфигурации может быть следующим:
14-
*
15-
* $form->field($model, 'contacts')->widget(MultipleInput::className(), [
16-
* 'limit' => 4,
17-
* 'name' => $model->formName() . '[contacts]',
18-
* 'data' => $model->contacts[UserContact::getTypeEnum(UserContact::TYPE_PHONE)],
19-
* 'columns' => [
20-
* [
21-
* 'name' => UserContact::getTypeEnum(UserContact::TYPE_PHONE),
22-
* 'value' => function ($data) {
23-
* return $data;
24-
* }
25-
* ]
26-
* ]
27-
* ]);
4+
In progress

assets/MultipleInputAsset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class MultipleInputAsset extends AssetBundle
2424

2525
public function init()
2626
{
27-
$this->sourcePath = __DIR__ . '/assets/src/';
27+
$this->sourcePath = __DIR__ . '/src/';
2828
parent::init();
2929
}
3030

assets/src/js/jquery.multipleInput.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,25 @@
1010
}
1111
};
1212

13+
var defaultOptions = {
14+
group_id: null,
15+
template: null,
16+
btn_action: null,
17+
btn_type: null,
18+
limit: 1,
19+
replacement: []
20+
};
21+
1322
var methods = {
1423
init: function (options) {
15-
var settings = $.extend({}, options || {});
24+
var settings = $.extend(defaultOptions, options || {});
1625

17-
$(document).on('click', '.js-' + settings.group_id + '-input-remove', function (e) {
26+
$(document).on('click.multipleinput', '.js-' + settings.group_id + '-input-remove', function (e) {
1827
e.preventDefault();
1928
methods.removeInput.apply(this);
2029
});
2130

22-
$(document).on('click', '.js-'+ settings.group_id + '-input-plus', function (e) {
31+
$(document).on('click.multipleinput', '.js-'+ settings.group_id + '-input-plus', function (e) {
2332
e.preventDefault();
2433
methods.addInput.apply(this,[settings]);
2534
});
@@ -33,8 +42,8 @@
3342

3443
addInput: function (settings) {
3544
var template = settings.template,
36-
wrapper = $(this).parents('.multiple-input-list'),
37-
index = wrapper.find('.multiple-input-list__item').length,
45+
$wrapper = $(this).parents('.multiple-input-list').first(),
46+
index = $wrapper.find('.multiple-input-list__item').length,
3847
btn_action = settings.btn_action,
3948
btn_type = settings.btn_type,
4049
replacement = settings.replacement || [];
@@ -54,7 +63,8 @@
5463
template = template.replaceAll('{' + j + '}', replacement[j]);
5564
}
5665

57-
$(template).hide().appendTo(wrapper).fadeIn(300);
66+
console.log(template);
67+
$(template).hide().appendTo($wrapper).fadeIn(300);
5868
$(template).find('input, select, textarea').each(function () {
5969
methods.addAttribute.apply(this);
6070
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace unclead\widgets\examples\actions;
4+
5+
use Yii;
6+
use yii\base\Action;
7+
use unclead\widgets\examples\models\ExampleModel;
8+
9+
/**
10+
* Class MultipleInputAction
11+
* @package unclead\widgets\examples\actions
12+
*/
13+
class MultipleInputAction extends Action
14+
{
15+
public function run()
16+
{
17+
Yii::setAlias('@unclead-examples', realpath(__DIR__ . '/../'));
18+
$model = new ExampleModel();
19+
20+
if ($model->load(Yii::$app->request->post())) {
21+
if (!$model->validate()) {
22+
var_dump($model->getErrors());
23+
}
24+
25+
}
26+
return $this->controller->render('@unclead-examples/views/example.php', ['model' => $model]);
27+
}
28+
}

examples/models/ExampleModel.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace unclead\widgets\examples\models;
4+
5+
use yii\base\Model;
6+
use yii\validators\EmailValidator;
7+
use yii\validators\NumberValidator;
8+
9+
/**
10+
* Class ExampleModel
11+
* @package unclead\widgets\examples\actions
12+
*/
13+
class ExampleModel extends Model
14+
{
15+
const TYPE_EMAIL = 'email';
16+
const TYPE_PHONE = 'phone';
17+
18+
public $emails;
19+
20+
public $phones;
21+
22+
23+
public function rules()
24+
{
25+
return [
26+
['emails', 'validateEmails'],
27+
['phones', 'validatePhones']
28+
];
29+
}
30+
31+
public function attributes()
32+
{
33+
return [
34+
'emails',
35+
'phones'
36+
];
37+
}
38+
39+
public function scenarios()
40+
{
41+
return [
42+
self::SCENARIO_DEFAULT => ['emails', 'phones']
43+
];
44+
}
45+
46+
public function validatePhones($attribute)
47+
{
48+
$items = $this->$attribute;
49+
50+
if (!is_array($items)) {
51+
$items = [];
52+
}
53+
54+
$multiple = true;
55+
if(!is_array($items)) {
56+
$multiple = false;
57+
$items = (array) $items;
58+
}
59+
60+
foreach ($items as $index => $item) {
61+
$validator = new NumberValidator();
62+
$error = null;
63+
$validator->validate($item, $error);
64+
if (!empty($error)) {
65+
$key = $attribute . ($multiple ? '[' . $index . ']' : '');
66+
$this->addError($key, $error);
67+
}
68+
}
69+
}
70+
71+
public function validateEmails($attribute)
72+
{
73+
$items = $this->$attribute;
74+
75+
if (!is_array($items)) {
76+
$items = [];
77+
}
78+
79+
$multiple = true;
80+
if(!is_array($items)) {
81+
$multiple = false;
82+
$items = (array) $items;
83+
}
84+
85+
foreach ($items as $index => $item) {
86+
$validator = new EmailValidator();
87+
$error = null;
88+
$validator->validate($item, $error);
89+
if (!empty($error)) {
90+
$key = $attribute . ($multiple ? '[' . $index . ']' : '');
91+
$this->addError($key, $error);
92+
}
93+
}
94+
}
95+
}

examples/views/example.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use yii\bootstrap\ActiveForm;
4+
use unclead\widgets\Button;
5+
use unclead\widgets\MultipleInput;
6+
use unclead\widgets\examples\models\ExampleModel;
7+
8+
/* @var $this \yii\base\View */
9+
/* @var $model ExampleModel */
10+
?>
11+
12+
<?php $form = ActiveForm::begin([
13+
'enableAjaxValidation' => false,
14+
'enableClientValidation' => false,
15+
'validateOnChange' => false,
16+
'validateOnSubmit' => true,
17+
'validateOnBlur' => false,
18+
]);?>
19+
20+
<?= $form->field($model, 'emails')->widget(MultipleInput::className(), [
21+
'limit' => 2,
22+
]);
23+
?>
24+
<?= Button::update();?>
25+
<?php ActiveForm::end();?>

0 commit comments

Comments
 (0)