Completed
Push — 2.1 ( c952e8...98ed49 )
by Carsten
10:00
created

InputWidget   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 48
rs 10
c 0
b 0
f 0
ccs 0
cts 14
cp 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 10 5
A hasModel() 0 4 2
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
 * @author Qiang Xue <[email protected]>
33
 * @since 2.0
34
 */
35
class InputWidget extends Widget
36
{
37
    /**
38
     * @var Model the data model that this widget is associated with.
39
     */
40
    public $model;
41
    /**
42
     * @var string the model attribute that this widget is associated with.
43
     */
44
    public $attribute;
45
    /**
46
     * @var string the input name. This must be set if [[model]] and [[attribute]] are not set.
47
     */
48
    public $name;
49
    /**
50
     * @var string the input value.
51
     */
52
    public $value;
53
    /**
54
     * @var array the HTML attributes for the input tag.
55
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
56
     */
57
    public $options = [];
58
59
60
    /**
61
     * Initializes the widget.
62
     * If you override this method, make sure you call the parent implementation first.
63
     */
64
    public function init()
65
    {
66
        if ($this->name === null && !$this->hasModel()) {
67
            throw new InvalidConfigException("Either 'name', or 'model' and 'attribute' properties must be specified.");
68
        }
69
        if (!isset($this->options['id'])) {
70
            $this->options['id'] = $this->hasModel() ? Html::getInputId($this->model, $this->attribute) : $this->getId();
71
        }
72
        parent::init();
73
    }
74
75
    /**
76
     * @return boolean whether this widget is associated with a data model.
77
     */
78
    protected function hasModel()
79
    {
80
        return $this->model instanceof Model && $this->attribute !== null;
81
    }
82
}
83