-
Notifications
You must be signed in to change notification settings - Fork 5
/
SimpleFormBehavior.php
131 lines (111 loc) · 3.7 KB
/
SimpleFormBehavior.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
namespace developeruz\behaviors;
/**
* SimpleFormBehavior for Yii2
*
* @author Elle <elleuz@gmail.com>
* @version 0.1
* @package Behaviors for Yii2
*
*/
use Yii;
use yii\behaviors\AttributeBehavior;
use yii\db\ActiveRecord;
use yii\helpers\Html;
class SimpleFormBehavior extends AttributeBehavior
{
public $scenario, $resultFormField, $formOption;
public $formMethod = 'post';
public $formAction = '';
public $config = [
self::TypePassword => [],
self::TypeHidden => [],
self::TypeFile => [],
self::TypeBoolean => [],
self::TypeDropDown => []
];
public $widget = [];
const TypeString = 'string';
const TypeText = 'text';
const TypeHidden = 'hidden';
const TypePassword = 'password';
const TypeFile = 'file';
const TypeBoolean = 'boolean';
const TypeDropDown = 'dropDown';
private $form, $schema;
public function events()
{
return [
ActiveRecord::EVENT_AFTER_FIND => 'generateForm',
ActiveRecord::EVENT_INIT => 'generateForm',
ActiveRecord::EVENT_AFTER_VALIDATE => 'generateForm',
];
}
public function generateForm()
{
$formFields = array();
$this->owner->setScenario($this->scenario);
$this->form = Yii::createObject('developeruz\behaviors\components\DevActiveForm');
$this->schema = $this->owner->getTableSchema();
foreach ($this->owner->activeAttributes() as $field) {
$formFields[] = $this->renderField($field);
}
$result = $this->form->beginForm($this->formAction, $this->formMethod, $this->formOption);
$result .= implode(' ', $formFields);
$result .= $this->form->endForm();
$this->owner->{$this->resultFormField} = $result;
}
protected function renderField($fieldName)
{
$type = $this->checkCustomType($fieldName);
if (!$type) {
$type = $this->schema->columns[$fieldName]->type;
}
$field = $this->form->field($this->owner, $fieldName);
switch ($type) {
case self::TypeHidden:
$input = $field->hiddenInput();
break;
case self::TypePassword:
$input = $field->passwordInput();
break;
case self::TypeFile:
$input = $field->fileInput();
break;
case self::TypeBoolean:
$input = $field->checkbox();
break;
case self::TypeDropDown:
$input = $field->dropDownList($this->config['dropDown'][$fieldName]);
break;
case self::TypeText:
$input = $field->textarea();
break;
default:
$input = $field->textInput();
}
if (array_key_exists($fieldName, $this->widget)) {
$input->widget($this->widget[$fieldName]['class'], $this->widget[$fieldName]['config']);
}
if ($type != self::TypeHidden) {
$label = ($type == self::TypeBoolean) ? '' : Html::activeLabel($this->owner, $fieldName);
$error = implode(', ', $this->owner->getErrors($fieldName));
return "<div class='field {$type}'>
{$label}
{$input->parts['{input}']}
{$error}
</div>";
} else {
return $input->parts['{input}'];
}
}
protected function checkCustomType($fieldName)
{
foreach ($this->config as $type => $array) {
if (in_array($fieldName, $array) || array_key_exists($fieldName, $array)) {
return $type;
}
}
return false;
}
}