Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add InputWidget::createInputField() #14441

Merged
merged 2 commits into from
Jul 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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::renderInput()` 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;
}

/**
* 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.
*
* @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