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\Widget; |
12
|
|
|
use yii\base\Model; |
13
|
|
|
use yii\base\InvalidConfigException; |
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. |
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 Model the data model that this widget is associated with. |
41
|
|
|
*/ |
42
|
|
|
public $model; |
43
|
|
|
/** |
44
|
|
|
* @var string the model attribute that this widget is associated with. |
45
|
|
|
*/ |
46
|
|
|
public $attribute; |
47
|
|
|
/** |
48
|
|
|
* @var string the input name. This must be set if [[model]] and [[attribute]] are not set. |
49
|
|
|
*/ |
50
|
|
|
public $name; |
51
|
|
|
/** |
52
|
|
|
* @var string the input value. |
53
|
|
|
*/ |
54
|
|
|
public $value; |
55
|
|
|
/** |
56
|
|
|
* @var array the HTML attributes for the input tag. |
57
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
58
|
|
|
*/ |
59
|
|
|
public $options = []; |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Initializes the widget. |
64
|
|
|
* If you override this method, make sure you call the parent implementation first. |
65
|
|
|
*/ |
66
|
1 |
|
public function init() |
67
|
|
|
{ |
68
|
1 |
|
if ($this->name === null && !$this->hasModel()) { |
69
|
|
|
throw new InvalidConfigException("Either 'name', or 'model' and 'attribute' properties must be specified."); |
70
|
|
|
} |
71
|
1 |
|
if (!isset($this->options['id'])) { |
72
|
1 |
|
$this->options['id'] = $this->hasModel() ? Html::getInputId($this->model, $this->attribute) : $this->getId(); |
73
|
1 |
|
} |
74
|
1 |
|
parent::init(); |
75
|
1 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return bool whether this widget is associated with a data model. |
79
|
|
|
*/ |
80
|
1 |
|
protected function hasModel() |
81
|
|
|
{ |
82
|
1 |
|
return $this->model instanceof Model && $this->attribute !== null; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|