-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathActiveForm.php
142 lines (127 loc) · 3.56 KB
/
ActiveForm.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
<?php
/**
* This file is part of the yii2-uikit project.
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
* @copyright yii2-uikit (c) 2018
* @author Eugene Zakirov (worstinme) <[email protected]>
*/
namespace worstinme\uikit;
use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
/**
* A Uikit 3 enhanced version of [[\yii\widgets\ActiveForm]].
*
* This class mainly adds the [[layout]] property to choose a Uikit 3 form layout and [[grid]] property to add grid options to the form tag
* So for example to render a horizontal form you would:
*
* ```php
* use worstinme\uikit\ActiveForm;
*
* $form = ActiveForm::begin(['layout' => 'horizontal'])
* ```
*
* ```php
* use worstinme\uikit\ActiveForm;
*
* $form = ActiveForm::begin(['grid' => true])
* ```
*
* This will set default values for the [[ActiveField]]
* to render grid of fields. In particular the [[ActiveField::width]]
* is set to '1-1'
*
* To get a different column layout in grid mode you can modify those options
* through [[fieldConfig]]:
*
* ```php
* $form = ActiveForm::begin([
* 'grid' => true,
* 'fieldConfig' => [
* 'width' => "1-3@m"
* ],
* ]);
* ```
* or
*
* ```php
* $form->field($model,'attribute',['width'=>'auto@m']);
* ```
* or
*
* ```php
* $form = ActiveForm::begin([
* 'layout' => 'horizontal',
* 'options' => [
* 'class' => "uk-child-width-1-2@m uk-grid-small"
* ],
* ]);
* ```
*
* @see ActiveField for details on the [[fieldConfig]] options
* @see https://getuikit.com/docs/form
*
*/
class ActiveForm extends \yii\widgets\ActiveForm
{
/**
* {@inheritdoc}
*/
public $successCssClass = 'uk-form-success';
/**
* {@inheritdoc}
*/
public $errorCssClass = 'uk-form-danger';
/**
* {@inheritdoc}
*/
public $validationStateOn = self::VALIDATION_STATE_ON_INPUT;
/**
* @var string the default field class name when calling [[field()]] to create a new field.
* @see fieldConfig
*/
public $fieldClass = 'worstinme\uikit\ActiveField';
/**
* @var array HTML attributes for the form tag. Default is `[]`.
*/
public $options = [];
/**
* @var string the form layout. Either 'default', 'horizontal' or 'inline'.
* By choosing a layout, an appropriate default field configuration is applied. This will
* render the form fields with slightly different markup for each layout. You can
* override these defaults through [[fieldConfig]].
* @see \yii\bootstrap\ActiveField for details on Bootstrap 3 field configuration
*/
public $layout = 'horizontal';
/**
* @var bool if true, uikit grid options will be add to form and field tags
*/
public $grid = false;
/**
* {@inheritdoc}
*/
public function init()
{
if (!in_array($this->layout, ['horizontal', 'stacked'])) {
throw new InvalidConfigException('Invalid layout type: ' . $this->layout);
}
Html::addCssClass($this->options, 'uk-form');
if ($this->layout !== 'grid') {
Html::addCssClass($this->options, 'uk-form-' . $this->layout);
}
if ($this->grid) {
Html::addCssClass($this->options, 'uk-grid uk-child-width-1-1');
$this->options['uk-grid'] = true;
}
parent::init();
}
/**
* @return ActiveField
*/
public function field($model, $attribute, $options = [])
{
return parent::field($model, $attribute, $options);
}
}