Skip to content

Commit 3083574

Browse files
author
Eugene Tupikov
committed
adapted the code for correct work with widgets
1 parent 494602f commit 3083574

File tree

5 files changed

+69
-20
lines changed

5 files changed

+69
-20
lines changed

examples/actions/TabularInputAction.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
2+
23
/**
3-
* Created by PhpStorm.
4-
* User: unclead_2
5-
* Date: 15.08.2015
6-
* Time: 18:34
4+
* @link https://github.com/unclead/yii2-multiple-input
5+
* @copyright Copyright (c) 2014 unclead
6+
* @license https://github.com/unclead/yii2-multiple-input/blob/master/LICENSE.md
77
*/
88

99
namespace unclead\widgets\examples\actions;
@@ -15,28 +15,31 @@
1515
use yii\bootstrap\ActiveForm;
1616
use yii\web\Response;
1717

18+
/**
19+
* Class TabularInputAction
20+
* @package unclead\widgets\examples\actions
21+
*/
1822
class TabularInputAction extends Action
1923
{
2024
public function run()
2125
{
2226
Yii::setAlias('@unclead-examples', realpath(__DIR__ . '/../'));
2327

24-
$count = count(Yii::$app->request->post('Item', []));
2528
$models = [new Item()];
26-
for($i = 1; $i < $count; $i++) {
27-
$models[] = new Item();
28-
}
29-
3029
$request = Yii::$app->getRequest();
3130
if ($request->isPost && $request->post('ajax') !== null) {
31+
$data = Yii::$app->request->post('Item', []);
32+
foreach (array_keys($data) as $index) {
33+
$models[$index] = new Item();
34+
}
3235
Model::loadMultiple($models, Yii::$app->request->post());
3336
Yii::$app->response->format = Response::FORMAT_JSON;
3437
$result = ActiveForm::validateMultiple($models);
3538
return $result;
3639
}
3740

3841
if (Model::loadMultiple($models, Yii::$app->request->post())) {
39-
// put here your logic
42+
// your magic
4043
}
4144

4245

examples/models/Item.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<?php
22

3+
/**
4+
* @link https://github.com/unclead/yii2-multiple-input
5+
* @copyright Copyright (c) 2014 unclead
6+
* @license https://github.com/unclead/yii2-multiple-input/blob/master/LICENSE.md
7+
*/
8+
39
namespace unclead\widgets\examples\models;
410

511
use Yii;
@@ -42,7 +48,8 @@ public function behaviors()
4248
public function rules()
4349
{
4450
return [
45-
[['title', 'description'], 'required']
51+
[['title', 'description'], 'required'],
52+
['file', 'safe']
4653
];
4754
}
4855

examples/views/tabular.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
'validateOnChange' => false,
1717
'validateOnSubmit' => true,
1818
'validateOnBlur' => false,
19+
'options' => [
20+
'enctype' => 'multipart/form-data'
21+
]
1922
]) ?>
2023

2124
<?= TabularInput::widget([
@@ -41,6 +44,11 @@
4144
'name' => 'file',
4245
'title' => 'File',
4346
'type' => \vova07\fileapi\Widget::className(),
47+
'options' => [
48+
'settings' => [
49+
'url' => ['site/fileapi-upload']
50+
]
51+
]
4452
],
4553
[
4654
'name' => 'date',

src/TabularColumn.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,25 @@ public function getElementName($index, $withPrefix = true)
3434
return $prefix . $elementName;
3535
}
3636

37-
37+
/**
38+
* Returns first error of the current model.
39+
*
40+
* @param $index
41+
* @return string
42+
*/
3843
public function getFirstError($index)
3944
{
4045
return $this->getModel()->getFirstError($this->name);
4146
}
4247

48+
/**
49+
* Ensure that model is an instance of yii\base\Model.
50+
*
51+
* @param $model
52+
* @return bool
53+
*/
4354
protected function ensureModel($model)
4455
{
4556
return $model instanceof Model;
4657
}
47-
48-
4958
}

src/components/BaseColumn.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,14 @@ public function setModel($model)
113113
}
114114
}
115115

116-
117116
protected function ensureModel($model)
118117
{
119118
return true;
120119
}
121120

122-
121+
/**
122+
* @inheritdoc
123+
*/
123124
public function init()
124125
{
125126
parent::init();
@@ -337,16 +338,37 @@ protected function renderDefault($name, $value, $options)
337338
Html::addCssClass($options, 'form-control');
338339
$input = Html::$type($name, $value, $options);
339340
} elseif (class_exists($type) && method_exists($type, 'widget')) {
340-
$input = $type::widget(array_merge($options, [
341-
'name' => $name,
342-
'value' => $value,
343-
]));
341+
$input = $this->renderWidget($type, $name, $value, $options);
344342
} else {
345343
throw new InvalidConfigException("Invalid column type '$type'");
346344
}
347345
return $input;
348346
}
349347

348+
protected function renderWidget($type, $name, $value, $options)
349+
{
350+
$model = $this->getModel();
351+
if ($model instanceof Model) {
352+
$widgetOptions = [
353+
'model' => $model,
354+
'attribute' => $this->name,
355+
'value' => $value,
356+
'options' => [
357+
'id' => $this->normalize($name),
358+
'name' => $name
359+
]
360+
];
361+
} else {
362+
$widgetOptions = [
363+
'name' => $name,
364+
'value' => $value
365+
];
366+
}
367+
$options = array_merge($options, $widgetOptions);
368+
return $type::widget($options);
369+
}
370+
371+
350372
/**
351373
* @param string $error
352374
* @return string

0 commit comments

Comments
 (0)