From 78d76c3f2c35c0bf71002440e8bf0f8ea9051e2f Mon Sep 17 00:00:00 2001 From: vladis Date: Sun, 24 Dec 2017 08:07:18 +0500 Subject: [PATCH 1/3] Enh #15226: Auto generate placeholder from fields --- framework/CHANGELOG.md | 2 +- framework/helpers/BaseHtml.php | 17 +++++++++++++++++ tests/framework/helpers/HtmlTest.php | 18 ++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 0d384c1bf8..17df08c2b8 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -3,7 +3,7 @@ Yii Framework 2 Change Log 2.0.14 under development ------------------------ - +- Enh #15226: Auto generate placeholder from fields (vladis84) - Bug #15356: Fixed multiple bugs in `yii\db\Query::getTablesUsedInFrom()` (vladis84, samdark) - Bug #14157: Add support for loading default value `CURRENT_TIMESTAMP` of MySQL `datetime` field (rossoneri) - Bug #15046: Throw an `yii\web\HeadersAlreadySentException` if headers were sent before web response (dmirogin) diff --git a/framework/helpers/BaseHtml.php b/framework/helpers/BaseHtml.php index 2907573fa2..14fb8a86c8 100644 --- a/framework/helpers/BaseHtml.php +++ b/framework/helpers/BaseHtml.php @@ -1359,9 +1359,25 @@ class BaseHtml public static function activeTextInput($model, $attribute, $options = []) { self::normalizeMaxLength($model, $attribute, $options); + self::activePlaceholder($model, $attribute, $options); return static::activeInput('text', $model, $attribute, $options); } + /** + * Generate placeholder from model label. + * @param Model $model the model object + * @param string $attribute the attribute name or expression. See [[getAttributeName()]] for the format + * about attribute expression. + * @param array $options the tag options in terms of name-value pairs. These will be rendered as + * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. + */ + protected static function activePlaceholder($model, $attribute, &$options = []) + { + if (isset($options['placeholder']) && $options['placeholder'] === true) { + $options['placeholder'] = $model->getAttributeLabel($attribute); + } + } + /** * Generates a hidden input tag for the given model attribute. * This method will generate the "name" and "value" tag attributes automatically for the model attribute @@ -1458,6 +1474,7 @@ class BaseHtml $options['id'] = static::getInputId($model, $attribute); } self::normalizeMaxLength($model, $attribute, $options); + self::activePlaceholder($model, $attribute, $options); return static::textarea($name, $value, $options); } diff --git a/tests/framework/helpers/HtmlTest.php b/tests/framework/helpers/HtmlTest.php index d045bd3306..9393377a73 100644 --- a/tests/framework/helpers/HtmlTest.php +++ b/tests/framework/helpers/HtmlTest.php @@ -1579,6 +1579,24 @@ HTML; $actual = Html::activeCheckboxList($model, 'types', ['foo']); $this->assertEqualsWithoutLE($expected, $actual); } + + public function testActiveTextInput_placeholderFillFromModel() + { + $model = new HtmlTestModel(); + + $html = Html::activeTextInput($model, 'name', ['placeholder' => true]); + + $this->assertContains('placeholder="Name"', $html); + } + + public function testActiveTextInput_customPlaceholder() + { + $model = new HtmlTestModel(); + + $html = Html::activeTextInput($model, 'name', ['placeholder' => 'Custom placeholder']); + + $this->assertContains('placeholder="Custom placeholder"', $html); + } } /** From e99c574a7cb9a9ea3954c86186b693d7a15454b9 Mon Sep 17 00:00:00 2001 From: vladis Date: Sun, 24 Dec 2017 08:38:53 +0500 Subject: [PATCH 2/3] Enh #15226: Auto generate placeholder from fields --- framework/helpers/BaseHtml.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/framework/helpers/BaseHtml.php b/framework/helpers/BaseHtml.php index 14fb8a86c8..7a4adedd0b 100644 --- a/framework/helpers/BaseHtml.php +++ b/framework/helpers/BaseHtml.php @@ -1314,6 +1314,8 @@ class BaseHtml $options['id'] = static::getInputId($model, $attribute); } + self::activePlaceholder($model, $attribute, $options); + return static::input($type, $name, $value, $options); } @@ -1359,7 +1361,6 @@ class BaseHtml public static function activeTextInput($model, $attribute, $options = []) { self::normalizeMaxLength($model, $attribute, $options); - self::activePlaceholder($model, $attribute, $options); return static::activeInput('text', $model, $attribute, $options); } From 03ab53e1fb2ea803f925a94434dd7e372c9d51b1 Mon Sep 17 00:00:00 2001 From: vladis Date: Sun, 24 Dec 2017 09:52:12 +0500 Subject: [PATCH 3/3] Rename method --- framework/helpers/BaseHtml.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/framework/helpers/BaseHtml.php b/framework/helpers/BaseHtml.php index 7a4adedd0b..2b4eef1552 100644 --- a/framework/helpers/BaseHtml.php +++ b/framework/helpers/BaseHtml.php @@ -1314,7 +1314,7 @@ class BaseHtml $options['id'] = static::getInputId($model, $attribute); } - self::activePlaceholder($model, $attribute, $options); + self::setActivePlaceholder($model, $attribute, $options); return static::input($type, $name, $value, $options); } @@ -1372,7 +1372,7 @@ class BaseHtml * @param array $options the tag options in terms of name-value pairs. These will be rendered as * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. */ - protected static function activePlaceholder($model, $attribute, &$options = []) + protected static function setActivePlaceholder($model, $attribute, &$options = []) { if (isset($options['placeholder']) && $options['placeholder'] === true) { $options['placeholder'] = $model->getAttributeLabel($attribute); @@ -1475,7 +1475,7 @@ class BaseHtml $options['id'] = static::getInputId($model, $attribute); } self::normalizeMaxLength($model, $attribute, $options); - self::activePlaceholder($model, $attribute, $options); + self::setActivePlaceholder($model, $attribute, $options); return static::textarea($name, $value, $options); }