From cdfd73a9ffbcda1eb3431ec19fa33850fb09ca7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonardo=20Tietb=C3=B6hl?= Date: Wed, 25 Feb 2015 09:47:35 -0300 Subject: [PATCH] Add maxlength option to activeTextInput Issue [#7440](https://github.com/yiisoft/yii2/issues/7440) --- framework/helpers/BaseHtml.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/framework/helpers/BaseHtml.php b/framework/helpers/BaseHtml.php index 5fa47a53fb..8434873a60 100644 --- a/framework/helpers/BaseHtml.php +++ b/framework/helpers/BaseHtml.php @@ -1171,6 +1171,18 @@ class BaseHtml */ public static function activeTextInput($model, $attribute, $options = []) { + if (!isset($options['maxlength'])) { + return static::activeInput('text', $model, $attribute, $options); + } + + $attrName = preg_replace('/[\[].*[\]]/U', '', $attribute); + foreach ($model->getActiveValidators($attrName) as $validator) { + if ($validator instanceof \yii\validators\StringValidator && $validator->max !== null) { + $options['maxlength'] = $validator->max; + break; + } + } + return static::activeInput('text', $model, $attribute, $options); }