InputWidget.php 3.4 KB

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