From 88766c4e21a240562d4328604fe4cf11be3076e4 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Wed, 12 Jul 2017 13:01:05 +0200 Subject: [PATCH 1/2] Add `InputWidget::renderInput()` to implement behavior described in class docs directly in the class itself instead of relying on subclasses to respect the description. fixes #14294 --- framework/CHANGELOG.md | 4 +++- framework/captcha/Captcha.php | 6 +----- framework/widgets/InputWidget.php | 27 ++++++++++++++++++++++++--- framework/widgets/MaskedInput.php | 6 +----- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index cbe680c3800..c2cb6729700 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -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::renderInput()` to move behavior described in `InputWidget` class docs to the class itself (cebe) + 2.0.12 June 05, 2017 -------------------- diff --git a/framework/captcha/Captcha.php b/framework/captcha/Captcha.php index b1cc6e48535..c797d88281b 100644 --- a/framework/captcha/Captcha.php +++ b/framework/captcha/Captcha.php @@ -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(); diff --git a/framework/widgets/InputWidget.php b/framework/widgets/InputWidget.php index f59b4ec52aa..c31aae1d950 100644 --- a/framework/widgets/InputWidget.php +++ b/framework/widgets/InputWidget.php @@ -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: @@ -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); + } + } } diff --git a/framework/widgets/MaskedInput.php b/framework/widgets/MaskedInput.php index 20630ed5b96..7949e046998 100644 --- a/framework/widgets/MaskedInput.php +++ b/framework/widgets/MaskedInput.php @@ -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); } /** From 9195c2d41db5ef9f6627283d01d02a6dcac822f4 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Thu, 13 Jul 2017 17:01:29 +0300 Subject: [PATCH 2/2] Updated description [skip ci] --- framework/widgets/InputWidget.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/widgets/InputWidget.php b/framework/widgets/InputWidget.php index c31aae1d950..0fc4ce91688 100644 --- a/framework/widgets/InputWidget.php +++ b/framework/widgets/InputWidget.php @@ -89,7 +89,7 @@ protected function hasModel() } /** - * Create a HTML input tag + * Render a HTML input tag * * This will call [[Html::activeInput()]] if the input widget is [[hasModel()|tied to a model]], * or [[Html::input()]] if not.