From d8331835555750f758ecd709eb4fbabc4afd1c75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B3=D0=BE=D1=80=D1=8C=20=D0=A2=D0=B0=D1=80=D0=B0?= =?UTF-8?q?=D1=81=D0=BE=D0=B2?= Date: Fri, 13 Aug 2021 05:46:42 +0500 Subject: [PATCH 1/2] fix BaseHtml::activeStaticControl value type --- src/BaseHtml.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BaseHtml.php b/src/BaseHtml.php index e307a1d..d72e242 100644 --- a/src/BaseHtml.php +++ b/src/BaseHtml.php @@ -73,7 +73,7 @@ abstract class BaseHtml extends \yii\helpers\Html $value = static::getAttributeValue($model, $attribute); } - return static::staticControl($value, $options); + return static::staticControl((string)$value, $options); } /** From 7c6dbc295dabbb0999e758d3de73bcf062373e2a Mon Sep 17 00:00:00 2001 From: Simon Karlen Date: Mon, 16 Aug 2021 09:48:14 +0200 Subject: [PATCH 2/2] added test, updated changelog, removed unnecessary typecasting (already done by method definition) --- CHANGELOG.md | 2 +- src/BaseHtml.php | 5 ++-- tests/ActiveFormTest.php | 52 ++++++++++++++++++++++++++++++++++++++++ tests/data/User.php | 3 +++ 4 files changed, 58 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f3fdf1..8641e87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ Yii Framework 2 bootstrap5 extension Change Log 2.0.2 under development ----------------------- -- no changes in this release. +- Bug #6: yii\bootstrap5\BaseHtml::staticControl(): Argument #1 ($value) must be of type string, int given (dicrtarasov) 2.0.1 August 11, 2021 diff --git a/src/BaseHtml.php b/src/BaseHtml.php index d72e242..1bb7213 100644 --- a/src/BaseHtml.php +++ b/src/BaseHtml.php @@ -49,7 +49,6 @@ abstract class BaseHtml extends \yii\helpers\Html public static function staticControl(string $value, array $options = []): string { static::addCssClass($options, 'form-control-plaintext'); - $value = (string)$value; $options['readonly'] = true; return static::input('text', null, $value, $options); @@ -64,7 +63,7 @@ abstract class BaseHtml extends \yii\helpers\Html * @return string generated HTML * @see staticControl() */ - public static function activeStaticControl(Model $model, string $attribute, $options = []): string + public static function activeStaticControl(Model $model, string $attribute, array $options = []): string { if (isset($options['value'])) { $value = $options['value']; @@ -77,7 +76,7 @@ abstract class BaseHtml extends \yii\helpers\Html } /** - * {@inheritdoc} + * {@inheritDoc} */ public static function radioList($name, $selection = null, $items = [], $options = []): string { diff --git a/tests/ActiveFormTest.php b/tests/ActiveFormTest.php index 7782109..7e0ad82 100644 --- a/tests/ActiveFormTest.php +++ b/tests/ActiveFormTest.php @@ -310,6 +310,58 @@ HTML; $this->assertContainsWithoutLE($expected4, $out); } + public function testStaticControlRendering() + { + ActiveForm::$counter = 0; + ob_start(); + $model = new User(); + $model->setAttributes([ + 'id' => 1, + 'firstName' => 'John', + 'lastName' => 'Doe' + ]); + $form = ActiveForm::begin([ + 'action' => '/some-action', + 'layout' => ActiveForm::LAYOUT_DEFAULT + ]); + echo $form->field($model, 'id')->staticControl(); + echo $form->field($model, 'firstName')->staticControl(); + echo $form->field($model, 'lastName')->staticControl(); + echo $form->field($model, 'username')->staticControl(); + ActiveForm::end(); + $out = ob_get_clean(); + + $expected = << + + + +
+ +HTML; + + $expected2 = << + + + +
+ +HTML; + $expected3 = << + + + +
+ +HTML; + + $this->assertContainsWithoutLE($expected, $out); + $this->assertContainsWithoutLE($expected2, $out); + $this->assertContainsWithoutLE($expected3, $out); + } + /** * Fixes #128 * @see https://github.com/yiisoft/yii2-bootstrap5/issues/128 diff --git a/tests/data/User.php b/tests/data/User.php index 0e068c4..ccda0e6 100644 --- a/tests/data/User.php +++ b/tests/data/User.php @@ -11,6 +11,7 @@ use yii\base\Model; class User extends Model { + public $id; public $firstName; public $lastName; public $username; @@ -22,6 +23,8 @@ class User extends Model public function rules() { return [ + ['id', 'integer'], + [['firstName', 'lastName'], 'string'], ['username', 'string', 'min' => 4], ['password', 'string', 'min' => 8, 'max' => '20'], [['username', 'password'], 'required']