ActiveForm.php
6.75 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
namespace thread\app\bootstrap;
use Yii;
use yii\helpers\{
Url, Html, ArrayHelper
};
use yii\web\JsExpression;
//
use kartik\switchinput\SwitchInput;
/**
* Class ActiveForm
*
* @package thread\app\bootstrap
* @author FilamentV <vortex.filament@gmail.com>
* @copyright (c), Thread
*/
class ActiveForm extends \yii\bootstrap\ActiveForm
{
public $fieldClass = ActiveField::class;
//
public $enableClientValidation = true;
public $enableAjaxValidation = true;
public $validateOnSubmit = true;
public $enableClientScript = true;
public $validationUrl = 'validation';
/**
*
*/
public function init()
{
$this->action = Url::current();
parent::init();
$this->validationUrl = Url::toRoute($this->validationUrl) . '?id=' . ((isset($_GET['id'])) ? $_GET['id'] : 0);
$view = $this->getView();
$view->registerJs($this->setSubmit());
}
/**
* @return JsExpression
*/
public function setSubmit()
{
return new JsExpression('$("#' . $this->getId() . ' button[type=submit]").on( "click", function() {
var formId = "#' . $this->getId() . '";
var url = "' . $this->validationUrl . '";
var is_valid = false;
if(url.length){
var dataForm = $(formId).serialize();
$.post(url, dataForm, function(data){
var obj = data;
var classExtError = ".help-block-error";
console.log(obj);
if(data.length == 0){
is_valid = true;
$(formId).submit();
}
$("#' . $this->getId() . '").find(".has-error").removeClass("has-error");
$("#' . $this->getId() . '").find(classExtError).html("");
for(attr in obj){
$("#"+attr).parents(".form-group").addClass("has-error");
$("#"+attr).parents(".form-group").find(classExtError).html(obj[attr]);
}
});
}
return is_valid;
});');
}
/**
* @param array $models
* @param null $attributes
* @return array
*/
public static function validateMultiple($models, $attributes = null)
{
$result = [];
/** @var Model $model */
foreach ($models as $model) {
$model->validate($attributes);
foreach ($model->getErrors() as $attribute => $errors)
$result[Html::getInputId($model, $attribute)] = $errors;
}
return $result;
}
/**
* @param $model
* @param $attribute
* @param array $options
* @return mixed
*/
public function text_line($model, $attribute, $options = [])
{
return $this->field($model, $attribute, $options)->placeholder(Html::encode($model->getAttributeLabel($attribute)))->textInput(['maxlength' => true]);
}
/**
* @param $model
* @param $attribute
* @param array $options
* @return mixed
*/
public function text_line_lang($model, $attribute, $options = [])
{
return $this->text_line($model, $attribute, $options)->sign_lang();
}
/**
* @param $model
* @param $attribute
* @param array $options
* @return mixed
*/
public function text($model, $attribute, $options = [])
{
return $this->field($model, $attribute)->placeholder(Html::encode($model->getAttributeLabel($attribute)))->textarea($options);
}
/**
* @param $model
* @param $attribute
* @param array $options
* @return mixed
*/
public function text_lang($model, $attribute, $options = [])
{
if (!isset($options['style'])) {
$options['style'] = '';
}
$options['style'] .= ';resize: none;';
return $this->field($model, $attribute)->placeholder(Html::encode($model->getAttributeLabel($attribute)))->sign_lang()->textarea($options);
}
/**
* @param $model
* @param $attribute
* @param array $options
* @return mixed
*/
public function text_editor($model, $attribute, $options = [])
{
return $this->field($model, $attribute, $options)->editor();
}
/**
* @param $model
* @param $attribute
* @param array $options
* @param string $thema
* @return mixed
*/
public function text_editor_lang($model, $attribute, $options = [], $thema = 'full')
{
$template = [
'template' => '<div class="input-group" style="text-align: left;">{label}<span class="input-group-addon" style="display: inline;">' . Yii::$app->language . '</span></div>{input}{error}{hint}'
];
return $this->field($model, $attribute, ArrayHelper::merge($template, $options))->editor($thema);
}
/**
* @param $model
* @param $attribute
* @param array $options
* @return mixed
*/
public function text_password($model, $attribute, $options = [])
{
return $this->field($model, $attribute, $options)->passwordInput();
}
/**
* @param $model
* @param $attribute
* @param array $options
* @return $this
*/
public function checkbox($model, $attribute, $options = [])
{
return $this->field($model, $attribute, $options)->checkbox();
}
/**
* @param $model
* @param $attribute
* @param array $options
* @return $this
*/
public function switcher($model, $attribute, $options = [])
{
return $this->field($model, $attribute, $options)->widget(SwitchInput::classname(), []);
}
/**
* Form submit buttons
*
* @param $model
* @param $view
* @return string
*/
public function submit($model, $view)
{
$view->registerJs("$('.action_save_and_exit').click(function(){
$('input[name=\"save_and_exit\"]').val(1);
// return false;
});");
$content = Html::beginTag('div', ['class' => 'row form-group', 'style' => ["margin-right" => '0px']]);
$content .= Html::beginTag('div', [
'class' => "text-right submit-panel-buttons",
]);
$content .= Html::hiddenInput('save_and_exit');
$content .= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Add and Exit') : Yii::t('app', 'Save and Exit'), ['class' => 'btn btn-success action_save_and_exit']);
$content .= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Add') : Yii::t('app', 'Save'), ['class' => 'btn btn-info']);
$content .= Html::a(Yii::t('app', 'Cancel'), $view->context->actionListLinkStatus, ['class' => 'btn btn-danger']);
$content .= Html::endTag('div');
$content .= Html::endTag('div');
return $content;
}
}