Skip to content

Commit

Permalink
Add InputWidget::createInputField()
Browse files Browse the repository at this point in the history
to implement behavior described in class docs directly in the class
itself instead of relying on subclasses to respect the description.

fixes #14294
  • Loading branch information
cebe committed Jul 13, 2017
1 parent 6fa48ea commit 23a850b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
4 changes: 3 additions & 1 deletion framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ Yii Framework 2 Change Log
- Enh #14389: Optimize `Validator::validateAttributes()` by calling `attributeNames()` only once (nicdnep)
- Enh #14105: Implemented a solution for retrieving DBMS constraints in `yii\db\Schema` (sergeymakinen)
- Enh #14417: Added configuration for headers in PHP files generated by `message/extract` command (rob006)
- Enh #13835: Added `yii\web\Request::getOrigin()` method that returns HTTP_ORIGIN of current CORS request (yyxx9988)
- Enh #13835: Added `yii\web\Request::getOrigin()` method that returns `HTTP_ORIGIN` of current CORS request (yyxx9988)
- Bug #14165: Set `_slave` of `Connection` to `false` instead of `null` in `close` method (rossoneri)
- Bug #14423: Fixed `ArrayHelper::merge` behavior with null values for integer-keyed elements (dmirogin)
- Enh #14294: Added `InputWidget::createInputField()` to move behavior described in `InputWidget` class docs to the class itself (cebe)


2.0.12 June 05, 2017
--------------------
Expand Down
6 changes: 1 addition & 5 deletions framework/captcha/Captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,7 @@ public function init()
public function run()
{
$this->registerClientScript();
if ($this->hasModel()) {
$input = Html::activeTextInput($this->model, $this->attribute, $this->options);
} else {
$input = Html::textInput($this->name, $this->value, $this->options);
}
$input = $this->renderInput('text');
$route = $this->captchaAction;
if (is_array($route)) {
$route['v'] = uniqid();
Expand Down
27 changes: 24 additions & 3 deletions framework/widgets/InputWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
/**
* InputWidget is the base class for widgets that collect user inputs.
*
* An input widget can be associated with a data model and an attribute,
* or a name and a value. If the former, the name and the value will
* be generated automatically.
* An input widget can be associated with a data [[model]] and an [[attribute]],
* or a [[name]] and a [[value]]. If the former, the name and the value will
* be generated automatically (subclasses may call [[renderInput()]] to follow this behavior).
*
* Classes extending from this widget can be used in an [[\yii\widgets\ActiveForm|ActiveForm]]
* using the [[\yii\widgets\ActiveField::widget()|widget()]] method, for example like this:
Expand Down Expand Up @@ -87,4 +87,25 @@ protected function hasModel()
{
return $this->model instanceof Model && $this->attribute !== null;
}

/**
* Create a HTML input tag
*
* This will call [[Html::activeInput()]] if the input widget is [[hasModel()|tied to a model]],
* or [[Html::input()]] if not.
*
* @param string $type the type of the input to create.
* @return string the HTML of the input field.
* @since 2.0.13
* @see Html::activeInput()
* @see Html::input()
*/
protected function renderInput($type)
{
if ($this->hasModel()) {
return Html::activeInput($type, $this->model, $this->attribute, $this->options);
} else {
return Html::input($type, $this->name, $this->value, $this->options);
}
}
}
6 changes: 1 addition & 5 deletions framework/widgets/MaskedInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,7 @@ public function init()
public function run()
{
$this->registerClientScript();
if ($this->hasModel()) {
echo Html::activeInput($this->type, $this->model, $this->attribute, $this->options);
} else {
echo Html::input($this->type, $this->name, $this->value, $this->options);
}
echo $this->renderInput($this->type);
}

/**
Expand Down

0 comments on commit 23a850b

Please sign in to comment.