forked from masihfathi/yii2-drag-drop-forms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FormBase.php
125 lines (106 loc) · 2.83 KB
/
FormBase.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
<?php
namespace masihfathi\form;
use yii\helpers\ArrayHelper;
class FormBase {
/**
* Filter fields array and retun only fields with attribute name (data fields)
*
* @param array $array Array represent elements of form
* @return array Only fields with attribute name
*/
public function onlyCorrectDataFields($array = []){
$data_fields = [];
foreach ($array as $key_row => $row) {
foreach ($row as $key => $v) {
$field = $array[$key_row][$key];
if (ArrayHelper::keyExists('name', $field) ){
array_push($data_fields, $field);
}
}
}
return $data_fields;
}
/**
* Generate table schema need to create table
*
* Get array with data fields and
* @param array $array Data fields
* @return array
*/
public static function tableSchema($array = []){
$schema['id'] = 'pk';
foreach ($array as $row) {
foreach ($row as $v) {
if ( isset($v['name']) ){
$schema[ $v['name'] ] = ($v['field'] === 'textarea') ? 'text' : 'string';
}
}
}
return $schema;
}
/**
* Return column type.
*
* Need to add column in table SQL
*
* @param array $field Data one field.
* @return null|string
*/
public function getColumnType($field){
if (!ArrayHelper::keyExists('field', $field)){
return null;
}
$type = [
'input' => [
'text' => 'string',
'email' => 'string',
'password' => 'string',
'date' => 'string',
'number' => 'integer',
'url' => 'string',
'tel' => 'string',
'url' => 'string',
'color' => 'string',
'range' => 'string',
'url' => 'string',
],
'textarea' => 'text',
'checkbox' => 'string',
'radio' => 'string',
'select' => 'string',
];
if ($field['field'] === 'input'){
return $type[ $field['field'] ][ $field['type'] ];
}
return $type[ $field['field'] ];
}
/**
* Return validation rule type.
* Need to dynamic model validation
* @param array $f Data field.
* @return null|array
*/
public function ruleType($f){
$types = ['input' => [
'text' => 'string',
'email' => 'email',
'password' => 'string',
'date' => 'date',
'number' => 'integer',
'url' => 'url',
'tel' => 'string',
'color' => 'string',
'range' => 'string',
],
'textarea' => 'string',
'checkbox' => 'string',
'radio' => 'string',
'select' => 'string',
];
if ($f['field'] === 'input'){
return $types[$f['field']][$f['type']];
}
return $types[$f['field']];
}
}
?>