-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathForm.php
executable file
·128 lines (110 loc) · 2.89 KB
/
Form.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
<?php namespace pceuropa\forms;
use Yii;
use yii\base\Widget;
use yii\base\DynamicModel;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use pceuropa\forms\FormBase;
use pceuropa\forms\models\FormModel;
use pceuropa\email\Send as SendEmail;
/**
* FormRender: Render form
* Two method form render: php or js (beta)
*
* @author Rafal Marguzewicz <[email protected]>
* @version 3.0.2
* @license MIT
*
* https://github.com/pceuropa/yii2-forum
* Please report all issues at GitHub
* https://github.com/pceuropa/yii2-forum/issues
*
* Usage example:
* ~~~
* echo \pceuropa\forms\form::widget([
* 'form' => ',{}'
* ]);
*
* echo \pceuropa\forms\form::widget([
* 'formId' => 1,
* ]);
* ~~~
* echo \pceuropa\forms\Form::widget([
* FormBuilder requires Yii 2
* http://www.yiiframework.com
* https://github.com/yiisoft/yii2
*
*/
class Form extends Widget {
/**
* @var int Id of form. If set, widget take data from FormModel.
* @see pceuropa\models\FormModel
*/
public $formId = null;
/**
* @var array|string JSON Object representing the form body
*/
public $body = '{}';
/**
* @var string Type render js|php
* @since 1.0
*/
public $typeRender = 'php';
/**
* Initializes the object.
* @return void
* @see Widget
*/
public function init() {
parent::init();
if (is_int($this->formId)) {
$form = FormModel::FindOne($this->formId);
$this->body = $form->body;
}
$this->body = Json::decode($this->body);
}
/**
* Executes the widget.
* @since 1.0
* @return function
*/
public function run() {
if ($this->typeRender === 'js') {
return $this->jsRender($this->body);
}
return $this->phpRender($this->body);
}
/**
* Render form by PHP and add rules
* TODO: each rule for checkbox
* @param array $form
* @return View Form
*/
public function phpRender($form) {
$data_fields = FormBase::onlyCorrectDataFields($form);
$DynamicModel = new DynamicModel(ArrayHelper::getColumn($data_fields, 'name'));
foreach ($data_fields as $v) {
if (isset($v["name"]) && $v["name"]) {
if (isset($v["require"]) && $v["require"]) {
$DynamicModel->addRule($v["name"], 'required');
}
$rule = FormBase::ruleType($v);
$DynamicModel->addRule($v["name"], $rule);
}
}
return $this->render('form_php', [
'form_body' => $form,
'model' => $DynamicModel
]);
}
/**
* Render form by JavaScript
* @param array $form
* @return View
*/
public function jsRender($form) {
return $this->render('form_js', ['form' => $form]);
}
}
?>