Browse Source

Fixes #7440: Added support to automatically set the `maxlength` attribute for `Html::activeTextInput()`

tags/2.0.3
Qiang Xue 10 years ago
parent
commit
0eb27981cd
  1. 1
      framework/CHANGELOG.md
  2. 25
      framework/helpers/BaseHtml.php
  3. 8
      framework/widgets/ActiveField.php

1
framework/CHANGELOG.md

@ -41,6 +41,7 @@ Yii Framework 2 Change Log
- Enh #7350: Added `yii\helpers\Html::$dataAttributes` to support customizing data attributes (Faryshta, qiangxue)
- Enh #7357: Refactored `yii\db\ColumnSchema` by adding `typecast()` method to decouple `phpTypecast()` from `dbTypecast()` (mcd-php, qiangxue)
- Enh #7361: The `trim` validator now works on the client side too (qiangxue)
- Enh #7440: Added support to automatically set the `maxlength` attribute for `Html::activeTextInput()` (llfm)
- Enh #7446: Added `Schema::DOUBLE` to represent ANSI SQL Double Precision type (samdark)
- Enh #7449: Added `encode` option to allow not encoding select options for `Html::dropDownList()` and `Html::listBox()` (yapi68, qiangxue)
- Enh: Added support to `yii\di\Container` to instantiate and configure an object that implements `yii\base\Configurable` (qiangxue)

25
framework/helpers/BaseHtml.php

@ -10,6 +10,7 @@ namespace yii\helpers;
use Yii;
use yii\base\InvalidParamException;
use yii\db\ActiveRecordInterface;
use yii\validators\StringValidator;
use yii\web\Request;
use yii\base\Model;
@ -1167,22 +1168,26 @@ 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()]].
* See [[renderTagAttributes()]] for details on how attributes are being rendered.
* The following special options are recognized:
*
* - maxlength: integer|boolean, when `maxlength` is set true and the model attribute is validated
* by a string validator, the `maxlength` option will take the value of [[\yii\validators\StringValidator::max]].
* This is available since version 2.0.3.
*
* @return string the generated input tag
*/
public static function activeTextInput($model, $attribute, $options = [])
{
if (!isset($options['maxlength'])) {
return static::activeInput('text', $model, $attribute, $options);
}
$attrName = Html::getAttributeName($attribute);
foreach ($model->getActiveValidators($attrName) as $validator) {
if ($validator instanceof \yii\validators\StringValidator && $validator->max !== null) {
$options['maxlength'] = $validator->max;
break;
if (isset($options['maxlength']) && $options['maxlength'] === true) {
unset($options['maxlength']);
$attrName = Html::getAttributeName($attribute);
foreach ($model->getActiveValidators($attrName) as $validator) {
if ($validator instanceof StringValidator && $validator->max !== null) {
$options['maxlength'] = $validator->max;
break;
}
}
}
return static::activeInput('text', $model, $attribute, $options);
}

8
framework/widgets/ActiveField.php

@ -341,7 +341,13 @@ class ActiveField extends Component
* @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 [[Html::encode()]].
*
* If you set a custom `id` for the input element, you may need to adjust the [[$selectors]] accordingly.
* The following special options are recognized:
*
* - maxlength: integer|boolean, when `maxlength` is set true and the model attribute is validated
* by a string validator, the `maxlength` option will take the value of [[\yii\validators\StringValidator::max]].
* This is available since version 2.0.3.
*
* Note that if you set a custom `id` for the input element, you may need to adjust the value of [[selectors]] accordingly.
*
* @return static the field object itself
*/

Loading…
Cancel
Save