|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link http://www.yiiframework.com/ |
|
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
|
5
|
|
|
* @license http://www.yiiframework.com/license/ |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace yii\widgets; |
|
9
|
|
|
|
|
10
|
|
|
use Yii; |
|
11
|
|
|
use yii\base\InvalidConfigException; |
|
12
|
|
|
use yii\base\Model; |
|
13
|
|
|
use yii\base\Widget; |
|
14
|
|
|
use yii\helpers\Html; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* InputWidget is the base class for widgets that collect user inputs. |
|
18
|
|
|
* |
|
19
|
|
|
* An input widget can be associated with a data [[model]] and an [[attribute]], |
|
20
|
|
|
* or a [[name]] and a [[value]]. If the former, the name and the value will |
|
21
|
|
|
* be generated automatically (subclasses may call [[renderInputHtml()]] to follow this behavior). |
|
22
|
|
|
* |
|
23
|
|
|
* Classes extending from this widget can be used in an [[\yii\widgets\ActiveForm|ActiveForm]] |
|
24
|
|
|
* using the [[\yii\widgets\ActiveField::widget()|widget()]] method, for example like this: |
|
25
|
|
|
* |
|
26
|
|
|
* ```php |
|
27
|
|
|
* <?= $form->field($model, 'from_date')->widget('WidgetClassName', [ |
|
28
|
|
|
* // configure additional widget properties here |
|
29
|
|
|
* ]) ?> |
|
30
|
|
|
* ``` |
|
31
|
|
|
* |
|
32
|
|
|
* For more details and usage information on InputWidget, see the [guide article on forms](guide:input-forms). |
|
33
|
|
|
* |
|
34
|
|
|
* @author Qiang Xue <[email protected]> |
|
35
|
|
|
* @since 2.0 |
|
36
|
|
|
*/ |
|
37
|
|
|
class InputWidget extends Widget |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* @var \yii\widgets\ActiveField active input field, which triggers this widget rendering. |
|
41
|
|
|
* This field will be automatically filled up in case widget instance is created via [[\yii\widgets\ActiveField::widget()]]. |
|
42
|
|
|
* @since 2.0.11 |
|
43
|
|
|
*/ |
|
44
|
|
|
public $field; |
|
45
|
|
|
/** |
|
46
|
|
|
* @var Model the data model that this widget is associated with. |
|
47
|
|
|
*/ |
|
48
|
|
|
public $model; |
|
49
|
|
|
/** |
|
50
|
|
|
* @var string the model attribute that this widget is associated with. |
|
51
|
|
|
*/ |
|
52
|
|
|
public $attribute; |
|
53
|
|
|
/** |
|
54
|
|
|
* @var string the input name. This must be set if [[model]] and [[attribute]] are not set. |
|
55
|
|
|
*/ |
|
56
|
|
|
public $name; |
|
57
|
|
|
/** |
|
58
|
|
|
* @var string the input value. |
|
59
|
|
|
*/ |
|
60
|
|
|
public $value; |
|
61
|
|
|
/** |
|
62
|
|
|
* @var array the HTML attributes for the input tag. |
|
63
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
|
64
|
|
|
*/ |
|
65
|
|
|
public $options = []; |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Initializes the widget. |
|
70
|
|
|
* If you override this method, make sure you call the parent implementation first. |
|
71
|
|
|
*/ |
|
72
|
2 |
|
public function init() |
|
73
|
|
|
{ |
|
74
|
2 |
|
if ($this->name === null && !$this->hasModel()) { |
|
75
|
|
|
throw new InvalidConfigException("Either 'name', or 'model' and 'attribute' properties must be specified."); |
|
76
|
|
|
} |
|
77
|
2 |
|
if (!isset($this->options['id'])) { |
|
78
|
2 |
|
$this->options['id'] = $this->hasModel() ? Html::getInputId($this->model, $this->attribute) : $this->getId(); |
|
79
|
|
|
} |
|
80
|
2 |
|
parent::init(); |
|
81
|
2 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return bool whether this widget is associated with a data model. |
|
85
|
|
|
*/ |
|
86
|
2 |
|
protected function hasModel() |
|
87
|
|
|
{ |
|
88
|
2 |
|
return $this->model instanceof Model && $this->attribute !== null; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Render a HTML input tag |
|
93
|
|
|
* |
|
94
|
|
|
* This will call [[Html::activeInput()]] if the input widget is [[hasModel()|tied to a model]], |
|
95
|
|
|
* or [[Html::input()]] if not. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $type the type of the input to create. |
|
98
|
|
|
* @return string the HTML of the input field. |
|
99
|
|
|
* @since 2.0.13 |
|
100
|
|
|
* @see Html::activeInput() |
|
101
|
|
|
* @see Html::input() |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function renderInputHtml($type) |
|
104
|
|
|
{ |
|
105
|
|
|
if ($this->hasModel()) { |
|
106
|
|
|
return Html::activeInput($type, $this->model, $this->attribute, $this->options); |
|
107
|
|
|
} else { |
|
108
|
|
|
return Html::input($type, $this->name, $this->value, $this->options); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|