-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathEBootstrapActiveForm.php
518 lines (467 loc) · 12.3 KB
/
EBootstrapActiveForm.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
<?php
/**
* Wrapper class for CActiveForm
* Apply bootstrap style to the form
*
* @author Tim Helfensdörfer <[email protected]>
* @version 1.0.0
* @package bootstrap.yiiwidgets
*/
class EBootstrapActiveForm extends CActiveForm {
/**
* Display a horizontal form
*
* Default: false
*
* @access public
* @var boolean
*/
public $horizontal = false;
/**
* Class for the wrapper around the input.
*
* @access public
* @var string
*/
public $controlClass = 'col-lg-8';
/**
* Class for the wrapper around the label.
*
* @access public
* @var string
*/
public $controlLabelClass = 'col-lg-4';
/**
* Error message css class
*
* Default: help-inline
*
* @access public
* @var string
*/
public $errorMessageCssClass = 'help-inline';
/**
* Class for the form group wrapper which wrappes the buttons
*
* @access public
* @var string
*/
public $actionOffsetClass = 'col-lg-offset-4 col-lg-8';
/**
* If it's set to false the EBootstrap css file will be included (specially for ajax validation errors)
* If it's set null no css file will be included
*
* @since 0.4.4
* @access public
* @var string|boolean
*/
public $cssFile = false;
/**
* Init the widget .
*
* @access public
* @return void
*/
public function init() {
if ($this->horizontal)
EBootstrap::mergeClass($this->htmlOptions, array('form-horizontal'));
if ($this->cssFile === false) {
$cssFile = dirname(__FILE__).'/css/bootstrap.css';
$this->cssFile = Yii::app()->getAssetManager()->publish($cssFile);
Yii::app()->clientScript->registerCssFile($this->cssFile);
}
parent::init();
}
/**
* Execute the widget.
*
* @access public
* @return void
*/
public function run() {
parent::run();
}
/**
* Begins a form group.
*
* Into the form group belongs the label, the input and the error.
*
* @param CModel $model The model
* @param string $attribute The attribute
*
* @access public
* @return string
*/
public function beginControlGroup($model, $attribute, $options = array()) {
$option = array();
$option['class'] = 'form-group';
$error = $model->getError($attribute);
if (!empty($error))
EBootstrap::mergeClass($option, array('has-error'));
EBootstrap::mergeClass($options, $option);
return EBootstrap::openTag('div', $options);
}
/**
* End of the form group.
*
* @access public
* @return string
*/
public function endControlGroup() {
return EBootstrap::closeTag('div');
}
/**
* Beginning of the controls (inputs).
*
* @access public
* @return string
*/
public function beginControls() {
if ($this->horizontal)
return EBootstrap::openTag('div', array('class' => $this->controlClass));
}
/**
* End of the controls.
*
* @access public
* @return string
*/
public function endControls() {
if ($this->horizontal)
return EBootstrap::closeTag('div');
}
/**
* Beginning of the form actions.
*
* Into the action sections belong all buttons like the submit or abort button.
*
* @access public
* @return string
*/
public function beginActions() {
$output = EBootstrap::openTag('div', array('class' => 'form-group'));
if ($this->horizontal)
$output .= EBootstrap::openTag('div', array('class' => $this->actionOffsetClass));
return $output;
}
/**
* End form actions.
*
* @access public
* @return string
*/
public function endActions() {
$output = EBootstrap::closeTag('div');
if ($this->horizontal)
$output .= EBootstrap::closeTag('div');
return $output;
}
/**
* Get the form error summary.
*
* Apply bootstrap style to the error summary
*
* @param CModel $model
* @param string $header
* @param string $footer
* @param array $htmlOptions
*
* @access public
* @return string
*/
public function errorSummary($model,$header=null,$footer=null,$htmlOptions=array()) {
return EBootstrap::errorSummary($model, $header, $footer, $htmlOptions);
}
/**
* Returns a HTML label.
*
* @param CModel $model The model
* @param string $attribute The attribute
* @param array $htmlOptions
*
* @access public
* @return string
*/
public function label($model,$attribute,$htmlOptions=array()) {
if ($this->horizontal)
EBootstrap::mergeClass($htmlOptions, array('control-label', $this->controlLabelClass));
else
EBootstrap::mergeClass($htmlOptions, array('control-label'));
return EBootstrap::activeLabel($model,$attribute,$htmlOptions);
}
/**
* Returns an advanced HTML label.
*
* @param CModel $model The model
* @param string $attribute The attribute
* @param array $htmlOptions
*
* @access public
* @return string
*/
public function labelEx($model,$attribute,$htmlOptions=array()) {
if ($this->horizontal)
EBootstrap::mergeClass($htmlOptions, array('control-label', $this->controlLabelClass));
else
EBootstrap::mergeClass($htmlOptions, array('control-label'));
return EBootstrap::activeLabelEx($model,$attribute,$htmlOptions);
}
/**
* Returns an text input field.
*
* @param CModel $model The model
* @param string $attribute The attribute
* @param array $htmlOptions
*
* @access public
* @return string
*/
public function textField($model,$attribute,$htmlOptions=array())
{
return EBootstrap::activeTextField($model,$attribute,$htmlOptions);
}
/**
* Returns an textarea.
*
* @param CModel $model The model
* @param string $attribute The attribute
* @param array $htmlOptions
*
* @access public
* @return string
*/
public function textArea($model,$attribute,$htmlOptions=array())
{
return EBootstrap::activeTextArea($model,$attribute,$htmlOptions);
}
/**
* Returns an password input field.
*
* @param CModel $model The model
* @param string $attribute The attribute
* @param array $htmlOptions
*
* @access public
* @return string
*/
public function passwordField($model,$attribute,$htmlOptions=array())
{
return EBootstrap::activePasswordField($model,$attribute,$htmlOptions);
}
/**
* Render an input field with a prepended text or icon.
*
* @param CModel $model The model
* @param string $attribute The attribute
* @param string $prepend The text or icon to prepend
* @param array $htmlOptions
*
* @access public
* @return string
*/
public function textFieldPrepend($model,$attribute,$prepend,$htmlOptions=array()) {
return EBootstrap::activeTextFieldPrepend($model, $attribute, $prepend, $htmlOptions);
}
/**
* Render an input field with a appended text or icon.
*
* @param CModel $model The model
* @param string $attribute The attribute
* @param string $append The text or icon to append
* @param array $htmlOptions
*
* @access public
* @return string
*/
public function textFieldAppend($model,$attribute,$append,$htmlOptions=array()) {
return EBootstrap::activeTextFieldAppend($model, $attribute, $append, $htmlOptions);
}
/**
* Render a password field with a prepended text or icon.
*
* @param CModel $model The model
* @param string $attribute The attribute
* @param string $prepend The text or icon to prepend
* @param array $htmlOptions
*
* @access public
* @return string
*/
public function passwordFieldPrepend($model,$attribute,$prepend,$htmlOptions=array()) {
return EBootstrap::activePasswordFieldPrepend($model, $attribute, $prepend, $htmlOptions);
}
/**
* Render a password field with a appended text or icon.
*
* @param CModel $model The model
* @param string $attribute The attribute
* @param string $append The text or icon to append
* @param array $htmlOptions
*
* @access public
* @return string
*/
public function passwordFieldAppend($model,$attribute,$append,$htmlOptions=array()) {
return EBootstrap::activePasswordFieldAppend($model, $attribute, $append, $htmlOptions);
}
/**
* Returns a help block.
*
* Help block can be used to improve the usabilty of a form.
*
* @param string $help Help message
*
* @access public
* @return string
*/
public function helpBlock($help) {
$html = EBootstrap::openTag('p', array('class' => 'help-block'));
$html .= $help;
$html .= EBootstrap::closeTag('p');
return $html;
}
/**
* Renders a dropdown list for a model attribute.
*
* @param CModel $model the data model
* @param string $attribute the attribute
* @param array $data data for generating the list options (value=>display)
* @param array $htmlOptions additional HTML attributes.
*
* @since 1.0.0
* @access public
* @return string
*/
public function dropDownList($model,$attribute,$data,$htmlOptions=array())
{
return EBootstrap::activeDropDownList($model,$attribute,$data,$htmlOptions);
}
/**
* Returns a inline help.
*
* Help inline is displayed right next to the input field (where the error should be displayed).
*
* @param string $help Help message
*
* @since 0.4.4
* @access public
* @return string
*/
public function helpInline($help) {
$html = EBootstrap::openTag('span', array('class' => 'help-inline'));
$html .= $help;
$html .= EBootstrap::closeTag('span');
return $html;
}
/**
* Begin a bootstrap form group.
*
* @since 0.5.0
* @access private
* @return string
*/
private function _beginBootstrapGroup($model, $attribute) {
$html = $this->beginControlGroup($model, $attribute);
$html .= $this->labelEx($model, $attribute);
$html .= $this->beginControls($model, $attribute);
return $html;
}
/**
* End a bootstrap form group.
*
* @since 0.5.0
* @access private
* @return string
*/
private function _endBootstrapGroup($model, $attribute) {
$html = $this->error($model, $attribute);
$html .= $this->endControls($model, $attribute);
$html .= $this->endControlGroup($model, $attribute);
return $html;
}
/**
* Create a textfield with a control group.
*
* @param CModel $model The model
* @param string $attribute The attribute
* @param array $htmlOptions HTML options for the text field
*
* @since 0.5
* @access public
* @return string html
*/
public function bootstrapTextField($model, $attribute, $htmlOptions = array()) {
EBootstrap::mergeClass($htmlOptions, array('form-control'));
$html = $this->_beginBootstrapGroup($model, $attribute);
$html .= $this->textField($model, $attribute, $htmlOptions);
$html .= $this->_endBootstrapGroup($model, $attribute);
return $html;
}
/**
* Create a textarea with a control group.
*
* @param CModel $model The model
* @param string $attribute The attribute
* @param array $htmlOptions HTML options for the textarea
*
* @since 0.5
* @access public
* @return string html
*/
public function bootstrapTextArea($model, $attribute, $htmlOptions = array()) {
EBootstrap::mergeClass($htmlOptions, array('form-control'));
$html = $this->_beginBootstrapGroup($model, $attribute);
$html .= $this->textArea($model, $attribute, $htmlOptions);
$html .= $this->_endBootstrapGroup($model, $attribute);
return $html;
}
/**
* Create a checkbox with a control group.
*
* @param CModel $model The model
* @param string $attribute The attribute
* @param array $htmlOptions HTML options for the checkbox
*
* @since 0.5
* @access public
* @return string html
*/
public function bootstrapCheckBox($model, $attribute, $htmlOptions = array()) {
$html = $this->_beginBootstrapGroup($model, $attribute);
$html .= $this->checkBox($model, $attribute, $htmlOptions);
$html .= $this->_endBootstrapGroup($model, $attribute);
return $html;
}
/**
* Create a textfield with a control group.
*
* @param CModel $model The model
* @param string $attribute The attribute
* @param array $htmlOptions HTML options for the text field
*
* @since 0.5
* @access public
* @return string html
*/
public function bootstrapPasswordField($model, $attribute, $htmlOptions = array()) {
EBootstrap::mergeClass($htmlOptions, array('form-control'));
$html = $this->_beginBootstrapGroup($model, $attribute);
$html .= $this->passwordField($model, $attribute, $htmlOptions);
$html .= $this->_endBootstrapGroup($model, $attribute);
return $html;
}
/**
* Render a submit buttom.
*
* @param string $label Label
* @param array $htmlOptions
*
* @access public
* @return EBootstrapButton
*/
public function submitButton($label, $htmlOptions = array()) {
EBootstrap::mergeClass($htmlOptions, array('btn', 'btn-primary'));
return EBootstrap::submitButton($label, null, null, null, null, null, $htmlOptions);
}
}
?>